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

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

问题描述

所以我试图访问 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() 返回 undefined?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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