0

Here is my code in client side

var us_exist=Meteor.call('isUserExists',function(e,r){});
console.log(us_exist)// displaying "undefined" in console
if(us_exist==0)
{
    console.log("user not exist")
    Meteor.call('updatePosts',this._id,option_data,function(e,r){});
}
else
{
    console.log("iser exists");
}

here is my method in server side

isUserExist: function()
{
 var u_exist=Polls_Coll.find({question:this.question}, {option1:{$elemMatch:{ids: u_name}}} );
return u_exist.count()
}

It is returning nothing.

When i run it in my browser console it is working fine and displaying result as 0 or 1.

Stennie
  • 63,885
  • 14
  • 149
  • 175
Sasikanth
  • 3,045
  • 1
  • 22
  • 45
  • This is a common gotcha, please have a look to this answer of mine : http://stackoverflow.com/questions/17460123/meteor-methods-returns-undefined/17460481#17460481 – saimeunt Feb 06 '14 at 10:09

1 Answers1

0

The value of the method is not returned from the call function, but rather passed as an argument for the callback. So you should put your code inside that empty callback you've got.

var self = this;

Meteor.call('isUserExists', function(e, us_exists) {

    console.log(us_exist);
    if(!us_exist) {
      console.log("user not exist")
      Meteor.call('updatePosts', self._id, ...);
    } else {
      console.log("user exists");
    }

});

 


 

By the way, get a habit of not using == operator in JavaScript. You'll be much happier without it in the long run.

Hubert OG
  • 19,314
  • 7
  • 45
  • 73