从服务器端流星方法返回值 [英] return values from server side meteor methods

查看:49
本文介绍了从服务器端流星方法返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,

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
                    })

                }
             });

        }

我想将 contact 返回给被调用的方法,因为它在一个内部函数中,我很难将它返回给被调用的方法.如果它在客户端,然后我们可以将值存储在会话变量中,我们可以返回它,但这是一个服务器端方法,如何做到这一点?

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?

推荐答案

Use Futures:

Use Futures:

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

Meteor.methods({

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

});

这篇关于从服务器端流星方法返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆