2017-09-13 19 views
-1

我想在“const amount_documents”中保存一个集合的文档数量。正如在这个问题中所描述的:如何获得猫鼬模型的所有计数?你不能简单地写如何用mongodb和node.js对文档进行计数?

const amount_documents = User.count(); 

什么是正确的方法来做到这一点?当我使用此代码:

var myCallback = User.count({}, function(err, count) { 
    callback(count); 
    }); 

它说:“回调没有定义”

+0

你在哪里定义'callback'?我怀疑你的函数运行正常,但没有回调引用。 Thry this and see: 'var myCallback = User.count({},function(err,count){console.log(count); });'' –

回答

1

User.count是异步的,这个语法,你有一个回调来执行你的代码,这种方式:

User.count({}, function(err, count) { 
    const amount_documents = count; 
    // your code using the count 
    }); 

如果您使用的承诺,并等待/异步语​​法,你可以做这样的:

const amount_documents = await User.count({}); 
// Your code using the count here 
相关问题