Meteor method not returning error or result value

前端 未结 2 1585
清酒与你
清酒与你 2021-01-28 07:28

Here is my server side code

Meteor.methods({
   addSupportRequest: function (support) {
    Support.insert(support, function (err, id) {
        if (err)
                


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-28 08:08

    Dsyko's answer was somewhat on the right track. However, the asynchronous callback will never pass its result to the scope of the original function, which has ended.

    What you want is to run the Support.insert operation synchronously, i.e. having the current fiber yield while I/O is happening. This is what the Meteor._wrapAsync function is for. Luckily for you, there is no need to do this manually because if you just take out the callback from the insert operation, it will run synchronously:

    Meteor.methods({
       addSupportRequest: function (support) {
         var id = Support.insert(support);
         // An error may be thrown here, but it certainly won't be a network error because the request will have already reached the server.
         console.log("Support request added: " + id);
         return id;
       });
    }
    

提交回复
热议问题