Ruben
Ruben

Reputation: 826

debugger stop after Meteor.call perhaps client/server debugging

Im stuck trying to debug a Meteor method in the server side. When debugging in the client (in Chrome) the debugger executes until the 4th line of the code below ("Meteor.call") and immediately return to the 2nd line ("convlist:function()") avoiding the 2nd debugger instruction. No error in client/server consoles. I also have the server side debugger but the running process never jump to it (Server debug in http://localhost:8080/debug?port=5858). Any advice will be appreciated.

Client Side:

Template.conversationList.helpers({
  convlist: function(){
    debugger;
    Meteor.call('getConvList', function(error, result){
    if(error){
      alert('Error');
    } else {
      debugger; // just to evaluate the result var
      return result;
    }
  });
  //edited 3rd debugger; 
  debugger;
}}); 

Server Side:

if (Meteor.isServer) {
  Meteor.methods({
    getConvList: function(){
      debugger;
      let myUser = new Array();
      myUser.push(Meteor.user()._id);
      var newConv = Conversations.aggregate([{ "$match" : { "users": {"$in":                         [Meteor.user()._id]}}}, { "$project": { lstmsg:1, "conversator": {"$setDifference": ["$users", myUser] }}}]);
      return newConv;
    }
  });
} 

Upvotes: 1

Views: 123

Answers (1)

Ricardo Pesciotta
Ricardo Pesciotta

Reputation: 393

  1. Why the method in server side return a value but it appears as undefined in client side? Reason: The returned value seems to be undefined in client debugging mode but the answer comes later from the server. Try to use Session.set variable to catch the returned value and to get dynamic update when the server answer comes.

  1. Why client debugger stops in 4th line?

On the client side, the method call is executed, and only passes the callback to be executed later - it's not run at the same moment the Method is called. And because the command debugger is just a shortcut for a breakpoint, it is not actually executed. So it's correct that the client only runs line 4, and then finished the convlist function.

  1. Code never jump to server side debugger:

Once you start "meteor debug", the server comes up paused by default. You must open the node inspector (http://localhost:8080/debug?port=5858) for it to unpause and launch your app. Note that Firefox doesn't load node inspector well, and might not unpause the server, in which case you will never see the line break on the server side.

  1. Why the method in server side return a value but it appears as undefined in client side?

The method call is asynchronous. So when you call it on the client side, it immediatelly returns undefined, and then starts executing on the server. Once it's done, the callback function is called, and you have access to the result value. You can store the result in the callback in a Session variable, and use it to display in your template - once it's returned from the server method call.

Additionally, make sure that the debugger instruction is inside the callback function you are passing to the method call. Remember that the correct method call is Method.call('methodName', inputParameter, callbackFunction); Try passing a null inputParameter, and check the result of the callback execution - this showed to be different for me. In case I pass no inputParameter, when the callback runs, both error and result are undefined, but if I pass any inputParam, then the function has the right parameter. Ex on the client side:

Template.conversationList.helpers({
  convlist: function(){
    debugger;
    Meteor.call('getConvList', null, function(error, result){
    if(error){
      alert('Error');
    } else {
      debugger; // just to evaluate the result var
      return result;
    }
  });
  //edited 3rd debugger; 
  debugger;
}}); 

Upvotes: 1

Related Questions