javascript - Callback is not a function in mongoose.find({}) -


i new node.js , mongoose, trying query objects mongo collection using find({}) , function 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 call function error saying

        return cb(err,quizes);                ^         typeerror: cb not function 

i stuck @ point, can please me this, in advance.

the callback should argument listallquizes, not argument 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, same this:

schema.statics.listallquizes = function listallquizes(cb) {   model.find({}, cb); }; 

here's example on how use it:

var quiz = app.model('quiz');  function home(req, res) {    quiz.listallquizes(function(err, quizes) {     if (err) return res.sendstatus(500);     (var = 0; < quizes.length; i++) {       console.log(quizes[i].quizname)     }     res.render('quiz', { quizlist : quizes });   }); }  

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -