Comment faire une requête à l'aide Mongoose, mais prendre ce document pour faire d'autres choses?
-
25-10-2019 - |
Question
User.findOne({}, function(err, doc){
//do stuff with "doc" here.
});
C'est la façon dont je fais actuellement il. Mais si je veux prendre « doc », et juste faire des choses.
User.findOne({}, function(err, doc){
//do stuff with "doc" here.
});
runAnotherFunction(doc.name)
doc...
Comment puis-je prendre « doc » de la partie « fonction »?
La solution
Node is event driven, it's not procedural code like you're probably used to. Before mongoose can finish retrieving a record execution can already be well passed the call the findOne. Anything that needs to be done with the doc should happen in the callback (The "function" part).
Autres conseils
you can try "step" , https://github.com/creationix/step
This makes it easier to control logic flow.
See this for example :
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow