如何使用 Mongoose 进行查询,然后使用该文档执行其他操作?
User.findOne({}, function(err, doc){
//do stuff with "doc" here.
});
这就是我目前的做法。但是如果我想拿“doc”,然后做点什么呢?
User.findOne({}, function(err, doc){
//do stuff with "doc" here.
});
runAnotherFunction(doc.name)
doc...
如何从“function”部分中取出“doc”?
User.findOne({}, function(err, doc){
//do stuff with "doc" here.
});
That's the way I currently do it. But what if I want to take "doc", and just do stuff.
User.findOne({}, function(err, doc){
//do stuff with "doc" here.
});
runAnotherFunction(doc.name)
doc...
How do I take "doc" out of the "function" part?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
只是偏爱你2024-11-11 09:32:23
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
节点是事件驱动的,它不是您可能习惯的过程代码。在 mongoose 完成检索记录之前,调用 findOne 已经可以很好地执行。需要对文档执行的任何操作都应该在回调中发生(“函数”部分)。
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).