我如何使用Mongoose进行查询,然后拿这个文档做其他事情?

User.findOne({}, function(err, doc){ //do stuff with "doc" here. }); 

这就是我现在这样做的方式。 但是,如果我想采取“文件”,只是做的东西。

 User.findOne({}, function(err, doc){ //do stuff with "doc" here. }); runAnotherFunction(doc.name) doc... 

如何从“function”部分取出“doc”?

节点是事件驱动的,它不像你可能习惯的程序代码。 在mongoose可以完成检索logging执行之前,已经可以很好的通过调用findOne。 任何需要用doc来完成的事情都应该在callback中发生(“function”部分)。

你可以尝试“步骤”, https://github.com/creationix/step

这使得更容易控制逻辑stream程。

看到这个例子:

asynchronous节点查询和处理结果