Returning value from callback within Meteor.method

前端 未结 4 2095
别那么骄傲
别那么骄傲 2020-12-15 13:28

I am running into something I don\'t understand with Meteor. I have this method, which takes a query, sends it to amazon, and then in the callback of that function I try to

4条回答
  •  生来不讨喜
    2020-12-15 13:48

    Meteor methods are asynchronous, you can get the result by many way.

    Using npm module fibers (The other answer are explaining it very clearly).

    There are some other way w/o using npm module :

    Via Session variable :

        Meteor.call('myMethod',args, function(error, result) { 
      if (error) { Session.set('result', error) } // Note that the error is returned synchronously 
      else { 
        Session.set('result', result) // Using : Session.get('result') will return you the result of the meteor call !
      }
    });
    

    Or Via template variable :

        Template.hello.onCreated(function helloOnCreated() {
      // counter starts at 0
      this.message = new ReactiveVar(0);
    });
    
    Template.hello.helpers({
      message() {
        return Template.instance().message.get();
      },
    });
    
    Template.hello.events({
      'click button'(event, instance) {
        Meteor.call('myMethod', args, function (error, result) {
          if (error) { Template.instance().message.set(error); }
          else {
            Template.instance().message.set(result);
          }
        })
      },
    });
    

    Hope it will help !

提交回复
热议问题