回调不是mongoose.find({})中的函数(Callback is not a function in mongoose.find({}))

编程入门 行业动态 更新时间:2024-10-05 19:18:20
回调不是mongoose.find({})中的函数(Callback is not a function in mongoose.find({}))

我是Node.js和猫鼬的新手,我试图使用find({})从mongo集合中查询对象,该函数如下所示:

schema.statics.listAllQuizes = function listAllQuizes(){ Model.find({},function(err,quizes,cb){ if(err){ return cb(err); }else if(!quizes){ return cb(); } else { return cb(err,quizes); } });};

但是当我调用这个函数时,我得到一个错误说

return cb(err,quizes); ^ TypeError: cb is not a function

我卡在这一点上,有人可以帮助我这一点,在此先感谢。

I am new to Node.js and mongoose, i am trying to query objects from a mongo collection using find({}) and the function is as follows :

schema.statics.listAllQuizes = function listAllQuizes(){ Model.find({},function(err,quizes,cb){ if(err){ return cb(err); }else if(!quizes){ return cb(); } else { return cb(err,quizes); } });};

But when i call this function i get an error saying

return cb(err,quizes); ^ TypeError: cb is not a function

I am stuck at this point, can someone please help me with this, thanks in advance.

最满意答案

该回调应该是listAllQuizes的参数,而不是匿名处理函数的参数。

换一种说法:

schema.statics.listAllQuizes = function listAllQuizes(cb) { Model.find({}, function(err, quizes) { if (err) { return cb(err); } else if (! quizes) { return cb(); } else { return cb(err, quizes); } }); };

从逻辑上讲,这几乎与此相同:

schema.statics.listAllQuizes = function listAllQuizes(cb) { Model.find({}, cb); };

这里有一个关于如何使用它的例子:

var quiz = App.model('quiz'); function home(req, res) { quiz.listAllQuizes(function(err, quizes) { if (err) return res.sendStatus(500); for (var i = 0; i < quizes.length; i++) { console.log(quizes[i].quizName) } res.render('quiz', { quizList : quizes }); }); }

The callback should an argument to listAllQuizes, not an argument to the anonymous handler function.

In other words:

schema.statics.listAllQuizes = function listAllQuizes(cb) { Model.find({}, function(err, quizes) { if (err) { return cb(err); } else if (! quizes) { return cb(); } else { return cb(err, quizes); } }); };

Which, logically, is almost the same as this:

schema.statics.listAllQuizes = function listAllQuizes(cb) { Model.find({}, cb); };

Here's an example on how to use it:

var quiz = App.model('quiz'); function home(req, res) { quiz.listAllQuizes(function(err, quizes) { if (err) return res.sendStatus(500); for (var i = 0; i < quizes.length; i++) { console.log(quizes[i].quizName) } res.render('quiz', { quizList : quizes }); }); }

更多推荐

本文发布于:2023-08-07 17:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   函数   mongoose   find   Callback

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!