R
R
RunFMe2015-03-08 19:20:22
JavaScript
RunFMe, 2015-03-08 19:20:22

How to get value from callback?

I use mongoose search in callback, in callback you can get the result of a database search using mongoose, now the question. How can you extract these results from the callback in order to use them further. Here is the code itself, I hope it will be clearer.
.validateRegistration(function (newUserAttrs, errors) {
var login = newUserAttrs.login;
var confirmPassword = newUserAttrs.confirmPassword;
if (!confirmPassword) errors.push('Missing password confirmation')
if (newUserAttrs.password != confirmPassword) errors. push(confirmPassword);
User.find({ login: login }, function (err, newUser) {
if (newUser || err) {
errors.push('Login already taken');//this is actually what I need
}
});
return errors;
})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SagePtr, 2015-03-08
@SagePtr

If something asynchronous is called in a function, then the function itself can only be asynchronous (since you cannot return something that does not yet exist and it will become known later). Therefore, make the function asynchronous and pass a callback to it, which is called after the errors array is finally formed (in this case, in the deepest nested callback)

D
Dmitry Avilov, 2015-03-08
@TheCreator

Read more about async or Q modules, your code looks a little scary now.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question