【问题标题】:Meteor returns Cursor object, but not able to .count()Meteor 返回 Cursor 对象,但不能 .count()
【发布时间】:2015-04-11 10:37:22
【问题描述】:

我不明白为什么这个游标返回零计数:

Template.productList.helpers({
    'products': function() {
        return Products.find({}, {transform: function(doc) {
            console.log(ProductsLocations.find({product_id: doc._id}).count());
            return doc;
        }});
    }
});

当我 console.log() 没有 .count() 时,我得到一个可以在 Chrome 开发工具中遍历的对象。

值存在于数据库中,doc._id 是有效 ID

【问题讨论】:

    标签: meteor


    【解决方案1】:

    你使用自动订阅吗?

    您可能需要确保订阅已准备就绪。请参阅Meteor: How can I tell when the database is ready?Displaying loader while meteor collection loads

    您确实在控制台中看到该对象的原因是,当您进行该调用时,订阅准备就绪。但是在您的帮助代码中,您可能假设订阅数据已经到达,而实际上它还没有到达。

    一个有用的调试工具是 Chrome 的设备模式(DevTools 中搜索图标附近的“电话”图标),它可以让您模拟慢速网络(例如 GPRS,每个请求有 500 毫秒的延迟)。

    详细了解Meteor's publish/subscribe 并查看https://www.discovermeteor.com/blog/template-level-subscriptions/ 了解最新模式。

    【讨论】:

    • 非常感谢!我在路由中声明了一个waitOn,瞧……数据准备好了。
    猜你喜欢
    • 2018-09-29
    • 2019-11-18
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多