为什么在客户端为Meteor.call()返回未定义? [英] Why is undefined being returned for Meteor.call() in client?

查看:68
本文介绍了为什么在客户端为Meteor.call()返回未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图访问Twitter REST API并检索一条推文的屏幕名称.我觉得我的代码会得到更好的解释:

So I'm attempting to access the twitter REST API and retrieve the screen name of a tweet. I feel like my code will be better explanation:

我正在从isClient()调用方法"screenName":

I'm calling the method 'screenName' from the isClient() :

  'click button': function () {
    Meteor.call('screenName',
      function(error,result) {
        if (error) {
          console.log(error);
        }
        else {
          window.alert(result);
        }
      }
    )
  }

由于某种原因,该方法在控制台上实际登录Twitter帐户的屏幕名称时会返回undefined.

And for some reason the method returns undefined when its actually logging in the screen name of the twitter account on the console.

Meteor.methods({
  'screenName': function() {
      T.get('search/tweets',
      {
        q:'#UCLA',
        count:1
      },
      function(err,data,response) {
        console.log(data.statuses[0].user.screen_name);
        return data.statuses[0].user.screen_name;
      }
    )
  }

如果有人可以帮助我.非常感谢!

If someone could please help me with this. Thank you so much!

推荐答案

您的服务器方法需要同步.该方法已返回undefined之后,该方法中的回调将返回.我想更具体一点,但是我不确定您使用的是哪个库.

Your server method needs to be synchronous. The callback in the method is returning after the method has already returned undefined. I'd like to be more specific but I'm not sure what library you are using.

通过查看 HTTP.call 文档.您的代码可能看起来像这样:

You can get a feel for this by looking at the examples from the HTTP.call documentation. Your code could look something like this:

Tget = Meteor.wrapAsync(T.get);

Meteor.methods({
  'screenName': function() {
    try {
      var result = Tget('search/tweets', {q:'#UCLA', count:1});
      return result.statuses[0].user.screen_name;
    } catch (e) {
      return false;
    }
  }
});

有关wrapAsync的更多信息,请参见文档.

See the docs for more information on wrapAsync.

这篇关于为什么在客户端为Meteor.call()返回未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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