从Mongoose调用了存储的javascript函数? [英] Called a stored javascript function from Mongoose?
问题描述
我在MongoDB实例中创建了一个存储的javascript函数,该函数对每个集合中的记录数进行计数.如果我进入mongo shell并输入:
I have created a stored javascript function in my MongoDB instance that counts the number of records in each collection. If I go to my mongo shell and type:
> db.eval("getTotals()");
它按预期工作.如果我这样尝试通过mongo调用它:
it works as expected. if I try to call it through mongo like so:
totals = mongoose.connection.db.eval("getTotals()");
console.log(totals);
undefined
被记录.有人在这里看到我在做什么错吗?
undefined
gets logged. Does anyone see what I am doing wrong here?
推荐答案
大多数猫鼬调用不会像这样以内联方式返回,而是希望传递一个回调来处理结果.
Most mongoose calls do not return in-line like this, but rather expect a callback to be passed in to process the results.
完全未经测试,但您可能想要以下内容:
Completely untested, but you probably want something like:
mongoose.connection.db.eval("getTotals()", function(err, retVal) {
console.log(retVal)
});
在现实世界中,将结果分配给该范围之外的var
或您想要执行的任何操作.
And in the real world, assign your result to a var
outside of that scope or whatever you want to do.
这篇关于从Mongoose调用了存储的javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!