Colton45
Colton45

Reputation: 270

Meteor helper erroring

I'm defining a simple helper:

ccFees: function(){
  var event = Events.findOne(this._id);
  return event
}

Which returns the object: [object Object]

But when I add a property, which exists, of the that object like so:

ccFees: function(){
  var event = Events.findOne(this._id);
  return event.cost
}

...it errors.

Exception from Deps recompute function: TypeError: Cannot read property 'cost' of undefined

Oddly enough, the info appears as intended on the first render, but if I modify the object for example after that initial creation, then it errors. Any help would be appreciated. This is a super routine task but it's bombing. Is this possibly Blaze related?

Upvotes: 0

Views: 60

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

The property does exist, but not the object. Database calls on the client side returns only what's already fetched via the subscription channel, so before your object is pulled from the server the findOne method can return null. I'm not sure why in your case this happens after the update, but that's the general idea.

The fix is simple: add a safeguard to check whether the object you're working on exists:

ccFees: function() {
  var event = Events.findOne(this._id);
  if(!event) return '';
  return event.cost;
},

In that simple case you can write it in the short form:

ccFees: function() {
  var event = Events.findOne(this._id);
  return event && event.cost;
},

Upvotes: 2

Related Questions