Answer the question
In order to leave comments, you need to log in
How to return data from model selected with findOne (mongoose)?
There is a controller that calls the model method to fetch data that needs to be returned back to the controller:
(model)
UsersSchema.methods.checkAuth = function(cookie) {
if(typeof cookie.crowder_main != 'undefined') {
this.model('Users').findOne({hash: cookie.crowder_main}).exec(function (err, user){
if (err) return err;
return user
})
}
}
Answer the question
In order to leave comments, you need to log in
Using Promise is very simple:
UsersSchema.methods.checkAuth = function(cookie) {
if(typeof cookie.crowder_main != 'undefined') {
var getUser = new Promise(function(resolve, reject) {
this.model('Users')
.findOne({hash: cookie.crowder_main})
.exec(function (err, user){
if (err) reject(err);
resolve(user)
});
})
getUser
.then(function(user) {
// user получен - продолжаем работать с ним в контроллере
})
.catch(function(err) {
// Обработка ошибки
})
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question