Answer the question
In order to leave comments, you need to log in
How to carry out data transfer in a chain of asynchronous functions?
I'm just getting started with Node.js. Due to asynchrony, I encountered a problem when working with the database.
For the database, I use mongoDB + mongoose on the node.
A little about architecture.
reqHandlers.js - here are methods for handling requests.
dbHandlers.js - here are methods for working with DB. These methods cannot affect the request in any way. Only return data from the database or error text.
Task: Get data from the database from the user and then somehow use them.
As a result, the following code was born:
reqHanders.js file - request processing
function login(req,res,next) {
var auth = req.body.auth;
var promise = new Q;
promise.then(dbHandlers.login(auth.login.toLowerCase(),auth.password))
.then(function makeSmt(user){
console.log("Q: "+user); //как организовать код, чтобы в переменной user лежали данные полученные из БД?
});
}
function login(login,password){
var connection = mongoose.connection;
if(!connection) return null;
var users = User.users;
users.find({'login':login,'password':password},function(err,user){
if(err){
console.error(err.stack);
//req.emit('error','userIncor');
return;
}
if(user.length === 0){
//req.emit('error','userIncor');
return;
}
//req.emit('success',user[0]);
return;
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question