从2个mongodb集合中search后,Nodejs进行单个callback

我有2个系列Stud and Prof。

我有一个函数,把id作为参数,并返回相应的信息,如果id属于任何这个集合。

第一个函数调用:传递属于Prof集合的id

第二个函数调用:传递属于Stud集合的id

预期成果:获得教学成果第一和螺柱结果第二

但是,由于asynchronous性质,我总是得到螺旋结果第一和教授结果第二。无论如何,通过引入一个新的variables或改变查询方式来完成这个任务吗?

任何帮助将不胜感激

var check_user_info = function(userid, callback) { Stud.findOne({ '_id': userid }, function(err, stud) { if (err) throw err if (stud) { callback(stud); } else { Prof.findOne({ '_id': userid }, function(err, prof) { if (err) throw err if (prof) { callback(prof); } else { callback(false); } }) } }) return } 

作为以前的答案,您可以使用asynchronous模块来完成此任务。 有很多function来控制节点的非阻塞性质。 在这里我会推荐你​​使用“并行”方法。 由于查询是相互独立的,所以比“瀑布”方法快。

根据你的问题,代码将如下所示。

 var async = require('async'); async.parallel([ cb=>{ Stud.findOne({ '_id': userid },cb); }, cb=>{ Prof.findOne({ '_id': userid },cb); } ],(err,result)=>{ if(err) { //handle error return; } //result will be an array where the first element will be the result of first query and // second element will be the query result for the second query // so according to this ..... if(result[0]){ //id is matched with Stud collection //result[0] is the student doc }else if(result[1]) { //id is matched with Prof collection //result[0] is the professor doc }else { //Neither Stud or Prof } }); 

你可以阅读asyn文档中的asynchronous方法

你可以使用asynchronous模式的瀑布方法来解决这个问题

 async.waterfall([ function(callback) { //your fist query method can go here callback(null, query_result1); }, function(first_result1, callback) { // your second query method go here callback(null, query_result2); } ], function (err, result) { // final result' }); 

对于你的问题的答案,应用@abdulbarik文章。

以下是关于您的实际代码的其他内容:


  • 把你的请求切成function
  • 当您使用callback时,请使用它们来正确返回错误。 不要丢。
  • 你不需要把_id键放在引号中

备注:

  • 由于您正在使用现在支持ES6(大部分)的node.js,请使用它。 简单阅读,更高效。

关于callback和function减less的示例。 我让你做其余的是es6,瀑布处理….你可以看看承诺和asynchronous/等待模式寿。

 // Check if there is a student function check_student(user_id, callback) { Stud.findOne({ _id: user_id }, function (err, stud) { if (err) return callback(err, false); // stud here can worth false return callback(false, stud); }); } 

 // Check if there is a prof function check_prof(user_id, callback) { Prof.findOne({ _id: user_id }, function (err, prof) { if (err) return callback(err, false); // prof here can worth false return callback(false, prof); }); } 

 // Get Stud not Prof info function check_user_info(user_id, callback) { // Look if user_id match a stud check_student(user_id, function (err, result) { // We have an error if (err) return callback(err, false); // We have a student if (result) return callback(false, result); // Check if user_id match a prof check_prof(user_id, function (err, result) { // We have an error if (err) return callback(err, false); // We have a prof if (result) return callback(false, result); // No result at all return callback(false, false); }); }); } 

你怎么称呼它

 check_user_info(user_id, function (err, result) { // ... }); 

具有承诺的代码示例:

  // Check if there is a student function check_student(user_id) { return new Promise((resolve, reject) => { Stud.findOne({ _id: user_id }, (err, stud) => { if (err) return reject(err); // prof here can worth false return resolve(stud); }); }); } // Check if there is a prof function check_prof(user_id) { return new Promise((resolve, reject) => { Prof.findOne({ _id: user_id }, (err, prof) => { if (err) return reject(err); // prof here can worth false return resolve(prof); }); }); } // Get Stud not Prof info function check_user_info(user_id) { return Promise.all([ check_student(user_id), check_prof(user_id), ]); } 

 check_user_info(user_id) .then([ stud, prof, ] => { // Handle result }) .catch((err) => { // Handle error });