为什么我的 Meteor.method 返回 undefined? [英] Why does my Meteor.method return undefined?

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

问题描述

有一个简单的字段可以输入 youtube ID.我使用的是renaldo 的youtube api (https://atmospherejs.com/renaldo/youtube-api)检索 youtube 剪辑的标题.

There is a simple field to input a youtube ID. I am using renaldo's youtube api (https://atmospherejs.com/renaldo/youtube-api) to retrieve the title of the youtube clip.

客户端事件成功地将轨道 ID (var tid) 值传递给方法addTrack"——它将轨道的标题输出到服务器上的控制台.我在将任何东西都退回给客户时真的很糟糕.

The client side event passes the track ID (var tid) value to the method 'addTrack' successfully - it outputs the track's title to the console on the server. I am having a really bad time returning anything at all back to the client.

如果我从 'addTrack' 方法调用 'getVideoData' 方法,它仍然每次都返回 'undefined'.我不是流星或 javascript 方面的专家,这只是我正在学习的东西.

If I call the 'getVideoData' method from the 'addTrack' method, it still returns 'undefined' every time. I am no expert in meteor or javascript, this is just something I am learning for the hell of it.

我了解回调的概念和 javascript 的同步性质(我认为!).

I understand the concept of callbacks and the synchronous nature of javascript (I think!).

谢谢.

if (Meteor.isServer) {

  YoutubeApi.authenticate({
    type: 'key',
    key: API_KEY
  });

Meteor.methods({
  addTrack: function(tid) {

  Meteor.call("getVideoData", tid, function(err,res) {

    console.log(res);

    });
  },
  getVideoData: function(tid) {
    var future = new Future();

    YoutubeApi.videos.list({
    part: "snippet",
    id: tid,
  }, function (err,data) {
      var _data = {"title":data.items[0].snippet.title,"desc":data.items[0].snippet.description};
      future["return"](_data)

    });
    return future.wait();

}

Meteor.startup(function () {Future = Npm.require('纤维/未来');

Meteor.startup(function () { Future = Npm.require('fibers/future');

});}

推荐答案

Meteor 方法很棘手,因为它们使用 Fibers 是同步的(好吧,它们在开发人员看来是同步的).因此,您需要使用 Meteor.wrapAsync() 将所有内容打包到 YoutubeApi.我还没有测试以下代码,但它应该类似于:

Meteor methods are tricky, as they use Fibers to be synchronous (well, they appear to the developer as synchronous). So you need to use Meteor.wrapAsync() to wrap the all to YoutubeApi. I haven't tested the following code, but it should look something like:

Meteor.methods({
  getVideoData: function(tid) {
    var syncYT = Meteor.wrapAsync(YoutubeApi.videos.list);
    var data = syncYT({part: "snippet",id: tid,});
    var transformed = {"title":data.items[0].snippet.title,"desc":data.items[0].snippet.description};

    console.log(transformed.title);
    return transformed.title;
  }
});

在这种情况下,您将需要阅读有关错误处理的更多信息,但这应该会让您有所了解.请记住,客户端 Meteor 始终是异步的,而服务器端是可选的异步.使用 wrapAsync 或 Futures 来处理方法中的异步需求.

You'll want to read more about error handling in this case, but this should get you going. Just remember that client-side Meteor is always asynchronous and server-side is optionally asynchronous. Use wrapAsync or Futures to handle async needs in methods.

最后,Meteor 指南很棒,使用它!

这篇关于为什么我的 Meteor.method 返回 undefined?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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