How to return value on Meteor.call() in client?

I have attempted to use a callback to get the return value of result but it keeps coming back undefined. No errors are occurring, so I can’t figure out why it’s not returning. Thoughts as to why?

And how you are returning it ?
Cause you cant use it directly as return value from helper.

I have the following

// on server
Meteor.methods({
  myMethod: function (arg1, arg2) {
    var result = getResult(arg1, arg2);
    return result;
  },
});

// on client
Meteor.call('myMethod', arg1, arg2, function(error, result) {
  if (error) {
    // handle error
  }
  else {
    console.log(result);
  }
});
2 Likes

and when u log it on server side before returning ?

Never mind. I figured out the issue. I’m using aldeed:simple-schema insert, which has callbacks for insert and update for validation purposes. I was returning it in the callback which obviously won’t work.

Regardless, i too would like to know how we are generally supposed to access the value returned by the Meteor.method we Meteor.call

my client:
`Template.admin.helpers({

isAdmin: function(){ 
    var result = Meteor.call('checkIfAdmin', function(error, result){return result;});
    console.log(result);
    return result; 
}

});`

and my server method
`Meteor.methods({

checkIfAdmin: function() {
    var isAdmin = Meteor.user().isAdmin;
    console.log(isAdmin);
    return isAdmin;
}

});`

where my Meteor.users has a callback hook for
Accounts.onCreateUser( function(options, user){ user.isAdmin = false; return user; });

for example…

i’ll report back when I find out, as we need to clarify this, as the docs show a … in the function body and it’s not precisely clear how the result is to be accessed on the client…console.log on the server is fine. on the client it’s undefined…

it appears i’m not the only one who thinks things are unclear…

So I tried a Meteor.apply with returnStubValue:true or something like this… because it’s an async call it still doesn’t have a value for the template helper… so i finally just put the method call out of the helper and used the session to store my returned method value…

Meteor.apply('checkIfAdmin', [],  function(error, result){ 
    if(result) Session.set('userIsAdmin', result); return; } );

Template.admin.helpers({
        isAdmin: function(){ 
        return Session.get('userIsAdmin'); 
    }
});

please excuse the Meteor.apply instead of Meteor.call… same same

All is well exceptyou mispelled function

Just to be clear the disputed topic here is in @Steve’s second suggested method, getting the result of the stub? But the first method, using ‘result’ in the callback is fine right?

callback is the way to go on the client

1 Like

or use this maybe?

I am having issues now with error handling and success messages, so maybe I have to use async await?