【问题标题】:Why asynchronous Meteor.call() doesn't return anything为什么异步 Meteor.call() 不返回任何内容
【发布时间】:2016-02-19 16:28:18
【问题描述】:

我在客户端有以下代码:

Meteor.call("getOldTests", m_user, test_id, course, language, function (error, result) {
            console.log("result: " + result);
            if (error) {
                Notifications.error(error.reason, 'We are working on this problem');
            } else {
                console.log(result);
                data.set(course, result);
            }

        });

其中服务器端有以下方法:

Meteor.methods({
    getOldTests: function (m_user, test_id, course, language) {
        var tests = Tests.findOne({email: m_user.email, course_en: course, test_id: test_id});
        if (tests) {
            console.log(tests);
            return Questions.find({course_en: course, variant: tests.variant, language: language});
        } else {
            return false;
        }
    },});

其中变量datareactive-dict

那么,为什么在客户端我的 Meteor.call() 函数内部没有执行任何操作(没有控制台输出),而实际上它调用了服务器端的方法(控制台输出中间结果)?

谢谢,

【问题讨论】:

  • 代码对我来说很好,复制应用程序或更多代码会有所帮助
  • 出于好奇,这里为什么使用方法而不是Meteor.publish()?看来你只是想返回一个光标。
  • @MichelFloyd 是的,你是对的!抱歉,我没弄明白。

标签: asynchronous meteor methods synchronization call


【解决方案1】:

看起来您正在使用更适合发布-订阅的方法:

服务器:

Meteor.publish('getOldTests',function (m_user, test_id, course, language) {
  var tests = Tests.findOne({email: m_user.email, course_en: course, test_id: test_id});
  if (tests) return Questions.find({course_en: course, variant: tests.variant, language: language});
  else this.ready();
});

客户:

Meteor.subscribe('getOldTests',m_user, test_id, course, language);

【讨论】:

    【解决方案2】:

    从服务器端获取的数据库和 API 调用应该包含在 Fiber 中。试试这个:

    Future = Npm.require('fibers/future');
    getOldTests: function (m_user, test_id, course, language) {
        var myFuture = new Future();
        var tests = Tests.findOne({email: m_user.email, course_en: course, test_id: test_id});
        if (tests) {
            console.log(tests);
            myFuture.return(Questions.find({course_en: course, variant: tests.variant, language: language}));
        } else {
            return false;
        }
     return myFuture.wait();
    }
    

    这个question 可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 2018-01-24
      • 1970-01-01
      • 2019-05-07
      • 2016-11-26
      • 2016-10-12
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      相关资源
      最近更新 更多