Method Call's callback return undefined. I did search before post

Hello to all,

Been searching this forum here and google about this, after few hours, still no idea why it doesn’t work.

I tried the following code but both return undefined:

Define Method 1:

Meteor.methods({
  playerChangeStatus: function(data){
     Players.update({_id:data.playerId}, {$set:{
       online: data.status
     }}, function (error, success) {
       console.log(success); // This print out number "1"
       if (error) {
         throw new Meteor.Error("update-error", "update-error");
       } else {
         return success
       }
     });
  }
});

Define Method 2:

Meteor.methods({
  playerChangeStatus: function(data){
    var success;
    var error;
     Players.update({_id:data.playerId}, {$set:{
       online: data.status
     }}, function (err, suc) {
       console.log(suc); // this print out number "1"
       error = err;
       success = suc;
     });
     if (error) {
       return error
     } else {
       return success
     }
  }
});

Meteor Call:

Meteor.call("playerChangeStatus", data, function(error, result) {
              console.log("Player change status callback triggered"); // this print without problem
              console.log("Error: " + error); // print undefined
              console.log("Result: " + result); // print undefined
                if (error) {
                    console.log("error", error); // as expected, didn't get triggered
                }
                if (result) {
                  console.log("driverStatusToggle ONLINE success: " + result); // as expected, didn't get triggered
                }
            });

I read many questions and answers regarding this issue and just unable to see what is wrong. I did read the documentation too. I follow the documentation on how the callback should look like.

Please advice, thank you.

try it this way:

Meteor.methods({
  playerChangeStatus: function(data){
     var success = Players.update({_id:data.playerId}, {$set:{
       online: data.status
     }};
     if (success) {
       return null, success;
     }  else throw new Meteor.Error("Something went wrong")
  }
});

On the server, if you run async functions (like the callback for the insert) Meteor won’t wait for them to complete unless you run them in a fiber or with async / await.

Running the insert without callback will make Meteor wait for the response before moving on. Any error thrown inside a method is returned to the client as the first param. If you want to return something else than error you can return null, somethingelse.

I actually don’t remember if insert / update etc throw any Errors, i think they do, but i always sanitise input before running them so i haven’t got an error in a very long time, might be wrong.

1 Like

Hi,

Thanks for the clarification, I really didn’t expect Meteor didn’t wait for the method to complete it’s callback before return.

Now I know method behave in such way, I’ll flow the code differently as per your suggestion. Thanks again.

Regards.

See it this way - if it is sync you would never need a callback.

1 Like