【问题标题】:Meteor.call callback is not executed and it's silently ignored if I don't do a fetchMeteor.call 回调未执行,如果我不执行提取,它会被静默忽略
【发布时间】:2015-12-16 04:47:50
【问题描述】:

为什么要写这个:

/client/Items.js

Template.Items.onCreated(function() {
  console.log('Methor.call');
  Meteor.call('Items.findAll', function (err, resp) {
    console.log('Methor.call callback');
    // Here I will use resp expecting it contains the response
    // returned by the method
    // ...
    return;
  });
  return;
});

/ItemsMethods.js

Meteor.methods({
  'Items.findAll': function () {
    return Items.find({});
  }
});

回调被静默忽略,即它没有被执行并且我没有收到任何错误?

请注意,如果我将这个return Items.find({}); 替换为这个return Items.find({}).fetch();,那么一切都会按预期工作。

【问题讨论】:

    标签: javascript meteor meteor-methods


    【解决方案1】:

    如果您在 Meteor method 中返回游标,则不会调用回调,因为游标不可序列化。正如documentation 所述,Meteor 方法应该返回一个EJSON-able 值或抛出异常。

    其实有一个feature request on GitHub,对这个问题的描述比较详细。

    【讨论】:

    • 谢谢。现在我明白了。我希望将来会为这些情况抛出异常(至少是服务器端)。
    【解决方案2】:

    Items.find({}); 返回一个游标,它是一种指向检索到的数据的指针。

    如果您使用Items.find({}).fetch();,您将返回一个对象数组。

    【讨论】:

    • 感谢您澄清这一点。我还是不明白为什么在第一种情况下Meteor.call的回调没有被执行?是否与方法返回的数据类型有关?
    猜你喜欢
    • 1970-01-01
    • 2021-06-22
    • 2013-09-09
    • 2019-11-17
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多