Exception in Template helper?


#1

This was going along fine and now I’m having an exception error pop up. Can someone explain why I don’t get exception error with this, and then suddenly I do get exception error. Then if I delete the code and re-do it I won’t get an error for a while and then: BOOM EXCEPTION ERROR!

  'user' : function(){
  var aUser = UserList.find().fetch()[0];
  console.log(aUser.name);
  var aUser = aUser.name;
  return aUser;
}

error:
xception in template helper: TypeError: Cannot read property ‘name’ of undefined
at Object.Template.hello.helpers.user (http://localhost:3000/v3.js?a2027bf3fb1d378aa84893eaec504af53766fc6d:15:24)
at bindDataContext (http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2880:16)
at Blaze._wrapCatchingExceptions (http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1651:16)
at http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2928:66
at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12)
at wrapHelper (http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2927:27)
at Spacebars.call (http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:172:18)
at Spacebars.mustacheImpl (http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:109:25)
at Object.Spacebars.mustache (http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:113:39)
at null._render (http://localhost:3000/template.v3.js?71758b4d9cf64358370ecc97b831d4566c80a396:12:22)

thanks,
Aaron


#2

sometimes your UserList.find().fetch()[0]; does not match any documents, so it returns undefined

than you try to access aUser.name and as aUser === undefined, error is thrown

And use findOne next time :smiley:

var aUser = UserList.findOne();

it should be same as your

var aUser = UserList.find().fetch()[0];

#3

Why wouldn’t it match something that exists? I mean, the documents are there. aUser.name gets output to the console and {{name}} renders in the page fine, so I don’t understand where the error is coming from.

As soon as I figure out how to use findOne() exactly, I will. :flushed:


#4

I believe it has to do with your subscription not being ready when that helper function is first being executed.
Since your sub isn’t ready your findOne() won’t match any data as there isn’t any. The reason why the name still shows up in the template and console, is that template helpers are basically Tracker.autorun and the findOne() creates a reactive dependency. So as soon as the data becomes available and your findOne() ‘sees’ the data, it will re-run.

Try to wait for the sub to be ready and you shouldn’t get an exception.


[Solved] Meteor.subscribe not receiving data
#5

If this is a template helper, this will do what you want:

'user' : function(){
  var aUser = UserList.findOne();
  return aUser && aUser.name;
}
return aUser && ...

is a check for existence of the user before returning.