Meteor: Meteor.call() from within observe callback does not execute

前端 未结 2 1252
萌比男神i
萌比男神i 2021-01-06 04:11

Is there any possibility of calling a server method from within a observe callback in Meteor?

I put together an example that reproduces the issue, that

相关标签:
2条回答
  • 2021-01-06 04:31

    This might be a known issue, i'm not sure since I've not tried it myself, but it looks like there might be a workaround (see https://github.com/meteor/meteor/issues/907)

    Add your Meteor.call into an instantaneous setTimeout callback:

    added: function(doc) {
        console.log("added "+doc.text);
        setTimeout(function() {
            Meteor.call('aMethod',doc.text,function(e,r){
                if(e){
                    console.log("error from server: "+e);
                }else{
                    console.log("response from server: "+r);
                }
            });
        },0);
    }
    
    0 讨论(0)
  • 2021-01-06 04:45

    Btw Tarang's answer also works when calling from a client method. Essentially I have a file in the client scope. And this file is a method definition. Inside my method definition I call a method at the server.

    I was having problems because only the client stub of the server call is being called and the server method does not get called. The fix was this one:

    Meteor.methods({  
        assignUserSession: function(options) {
            setTimeout(function() { // https://stackoverflow.com/questions/18645334/meteor-meteor-call-from-within-observe-callback-does-not-execute
                // Find all the base_accounts of this user and from there decide which view to prioritize.
                Meteor.call('user_base_accounts', {user_id: Meteor.userId()}, function(error,result){
                    ....
                    //server is then called due to logs shown in the command prompt
                    ....
                });
            });
        }
    });
    

    Thanks for pointing this out!

    0 讨论(0)
提交回复
热议问题