Callback model return date

35 views
Skip to first unread message

Felipo Antonoff

unread,
Feb 25, 2014, 9:14:47 PM2/25/14
to tot...@googlegroups.com
No model users have the following function that returns them, but the return is always undefinid and the console.log I get the normal data, anyone know how to solve.

//Retornando usuários
 exports.usuarios = function() {
db.find({ apelido: { $exists: true } }, function (err, docs) {
return docs;
});
};

I saw in another post that has to be a callback, but I could not make it work. A function that calls:
 self.model('usuario').usuarios();

Peter Širka

unread,
Feb 26, 2014, 10:32:08 AM2/26/14
to tot...@googlegroups.com
Hi Felipo,
it will not work because your model contains function with a callback:

exports.usuarios = function() {
db.find({ apelido: { $exists: true } }, function (err, docs) {
                // return WHERE?
return docs;
});
};

Solution:

exports.usuarios = function(cb) {

    db
.find({ apelido: { $exists: true } }, function (err, docs) {

        cb
(err, docs);
   
});
};

// CONTROLLER:
self.model('usuario').usuarios(function(err, docs) {
   
if (err) {
       
self.view500(err);
       
return;
   
}
   
self.json(docs);
});

Thanks :-)
Reply all
Reply to author
Forward
0 new messages