return values from server side meteor methods

柔情痞子 提交于 2019-12-25 07:58:23

问题


Here is my code,

googleContacts:function()
        {

            var opts= { email: Meteor.user().services.google.email,
              consumerKey: "xxxxxxxx",
              consumerSecret: "xxxxxxxxxx",
              token: Meteor.user().services.google.accessToken,
              refreshToken: Meteor.user().services.google.refreshToken};

            gcontacts = new GoogleContacts(opts);

            gcontacts.refreshAccessToken(opts.refreshToken, function (err, accessToken)
             {
                if(err)
                {
                    console.log ('gcontact.refreshToken, ', err);
                    return false;
                }
                else
                {
                    console.log ('gcontact.access token success!');
                    gcontacts.token = accessToken;
                    gcontacts.getContacts(function(err, contact) 
                    {
                      console.log(contact);
                       return contact;//want to return this value
                    })

                }
             });

        }

I want to return the contact to the called method,as it is in a inner function i'm getting a bit difficult to return it to the called method.If it is in client side,then we can store the value in a session variable and we can return that,but this is a server side method,How to do this?


回答1:


Use Futures:

Future = Npm.require('fibers/future');

Meteor.methods({

  methodname: function() {
    var fut = new Future();
    apiCall(function(err, res) {
      fut.return(...);
    });
    return fut.wait();
  },

});


来源:https://stackoverflow.com/questions/22760744/return-values-from-server-side-meteor-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!