javascript - Meteor method returns null on client, same method run on client returns proper value -


i got intended behavior, im confused why...

i have meteor method, returns value of document key; log statement proves such:

finduseremail: function(user_id){     var user = meteor.users.find({_id: user_id}).fetch();     console.log(user[0].emails[0].address);     return user[0].emails[0].address; } 

but when call on client, shared_user_email field null:

shared_user_email: meteor.call('finduseremail', $(ev.target).find('[name=shared_user]').val())

but, when simulate calling meteor method mimicking server query on client, returns value meteor method above logs:

shared_user_email: meteor.users.find({_id: $(ev.target).find('[name=shared_user]').val()}).fetch()[0].emails[0].address

what lost in translation when client tries calling server method?

edit

what happens when use meteor method insert document collection, field relies on meteor method? keep getting undefined shared_user_email field here:

var newlist = {         title: $(ev.target).find('[name=title]').val(),         description: $(ev.target).find('[name=description]').val(),         datecreated: today.todatestring(),         owner: meteor.userid(),         owner_email: meteor.users.find({_id: meteor.userid()}).fetch()[0].emails[0].address,         shared_user: $(ev.target).find('[name=shared_user]').val(),         shared_user_email: meteor.call('find_shared_user_email',              $(ev.target).find('[name=shared_user]').val(),              function(err, user_email){                 return user_email;             })     }      meteor.call('addlist', newlist, function(err, list){         return list;     }); 

on client, need invoke callback function of meteor.call() retrieve value method.

from documentation:

on client, if not pass callback , not inside stub, call return undefined, , have no way return value of method. because client doesn't have fibers, there not way can block on remote execution of method.

it should work follows:

meteor.call(   'finduseremail',   user_id,   function(error, result){     // 'result' here email address method   } ); 

Popular posts from this blog