javascript - Why do I receive undefined value after reloading page in Meteor? -
so have following form:
template(name='edituser') .row .col-md-4.col-md-offset-4 .page-header h1 edit user form#edit-user-form .form-group label(for='name') name input#user-name.form-control(type='text' placeholder='name' value='{{user.name}}') .form-group label(for='email') e-mail input#user-email.form-control(type='text' placeholder='e-mail' value='{{getemail user}}') button.btn.btn-primary(type='submit') update
the following handlebars.js-helper:
handlebars.registerhelper('getemail', function (user) { if (user.emails && user.emails[0] && user.emails[0].address) return user.emails[0].address; return ''; });
and following iron-router code:
editusercontroller = routecontroller.extend({ template: 'edituser', waiton: function () { return meteor.subscribe('user', this.params._id); }, data: function () { return { user: meteor.users.findone( { _id: this.params._id } ) }; } });
if run application , click on link edit-user-form can see e-mail address. if change code , meteor automatically refreshes page, e-mail-field empty , console says, can't fetch value of undefined.
if use same form, with-helper, e-mail displayed if meteor automatically refreshes page:
template(name='edituser') .row .col-md-4.col-md-offset-4 .page-header h1 edit user form#edit-user-form user .form-group label(for='name') name input#user-name.form-control(type='text' placeholder='name' value='{{name}}') .form-group label(for='email') e-mail input#user-email.form-control(type='text' placeholder='e-mail' value='{{getemail this}}') button.btn.btn-primary(type='submit') update
why so? , should use with-helper if single results (only 1 result display)?
thanks in advance!
replace meteor.users.findone
meteor.users.find
.
when findone
doesn’t find anything, returns undefined
causes error; when find
doesn’t find anything, returns empty cursor meteor knows with. doing adding with
cause meteor check if value undefined, check isn’t necessary cursor, empty or otherwise.
Comments
Post a Comment