MongoDB Shell确实返回值,但我的代码没有

时间:2016-10-24 22:46:22

标签: mongodb meteor

如果我在我的控制台输入,我会得到两个委托的结果。 Concole result

但试图在我的代码中获取一些数据失败。

let test = {};
    test.entry =  Posts.find().count();
    console.log(test.entry);

返回0!

我的收藏:

Posts = new Mongo.Collection('posts');

Posts.allow({
    insert: function () {
        return true;
    },
    update: function () {
        return true;
    },
    remove: function () {
        return true;
    }
});

我忘记了什么吗?
问题解决了。 忘了发布/订阅

2 个答案:

答案 0 :(得分:1)

试试这个:

let test = {};
test['entry'] = Posts.find().count();
console.log(test.entry)

答案 1 :(得分:0)

Posts.find().count() is asynchronous并且应该有一个回调参数。

尝试使用

Posts.find().count(function(err, count) {
  test.entry = count;
  console.log(test.entry);
});

这应该可以用于测试,但是当你在生产中实现它时,你显然必须检查err:)