回调不是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 }); }); }回调不是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 }); }); }