如何在Mongoose

时间:2016-10-20 04:55:16

标签: node.js mongodb mongoose

我是MongoDb和NodeJs的新手,我在nodejs中使用Mongoose,但是我遇到了一个问题,我使用第一个查询的结果对象进入嵌套查询但是未定义第一个查询对象。请给我任何建议。谢谢。

LuckyLunch.find(criteria, function (err, lunches) {   // First Query
   if (err) 
       return;
   var allLunches = lunches;   // Accurate result here
   for (var i = 0; i < allLunches.length; i++)
       {
         if (typeof allLunches[i].luckyMatch != 'undefined') {
              var usernames = [];
              Match.findOne(criteria2, function (err, singleMatch) {   // Second Query
                if (singleMatch != null) {
                     var userids = [];
                   for (var k = 0; k < singleMatch.participants.length; k++)
                   {
                      userids.push(ObjectId(singleMatch.participants[k]));
                   }
                      User.find(criteria, function (err, getusers) {  // Third Query
                      for (var j = 0; j < getusers.length; j++)
                          usernames.push(getusers[j].name);

                      allLunches[i].luckyUsers = usernames;  // allLunches[i] is undefined here.

                   });
                 }
              });
            }
          } 
      });

2 个答案:

答案 0 :(得分:2)

试试这个,

 var getData = function(callback) {
     LuckyLunch.find(criteria, function (err, lunches) {   // First Query
      if (err) {
         callback(err); 
         return;
      }        
      var len =   lunches.length; // try to use lenches only, Accurate result here
      for (var i = 0; i < len; i++)
       {
        (function(i) { 
         if (typeof lunches[i].luckyMatch != 'undefined') {
          var usernames = [];
          Match.findOne(criteria2, function (err, singleMatch) {   // Second Query
            if (singleMatch) {
                 var userids = [];
               for (var k = 0; k < singleMatch.participants.length; k++)
               {
                  userids.push(ObjectId(singleMatch.participants[k]));
               }
                  User.find(criteria, function (err, getusers) {  // Third Query
                  for (var j = 0; j < getusers.length; j++)
                      usernames.push(getusers[j].name);

                  lunches[i].luckyUsers = usernames;  // allLunches[i] is undefined here.

               });
             }
          });
        }
        })(i);
      }

      callback(null, "put here need data to return"); //this must be end of the ur loop
  });

 }

 getData(function(err, result){

   if(err)
     console.log(err);
     else
     console.log(result); 
 });

答案 1 :(得分:0)

经过一些改变,我得到了我的结果。我的答案是:

var getData = function (id,callback) {
      var criteria = {
          luckyMatch: { $exists: true },
          companyid: ObjectId(id)

      }
      LuckyLunch.find(criteria, function (err, lunches) {   // First Query
          var len = lunches.length; // try to use lenches only, Accurate result here
          for (var i = 0; i < len; i++) {
              (function (i) {
                  if (typeof lunches[i].luckyMatch != 'undefined') {
                      var usernames = [];
                      var criteria2 = {
                          participants: { $exists: true, $ne: [] },
                          _id: ObjectId(lunches[i].luckyMatch)
                      }
                      Match.findOne(criteria2, function (err, singleMatch) {   // Second Query
                          if (singleMatch) {
                              var userids = [];
                              var len2 = singleMatch.participants.length;
                              for (var k = 0; k < len2; k++) {
                                  userids.push(ObjectId(singleMatch.participants[k]));
                              }
                              var criteria3 = {
                                  _id: { $in: userids }
                              }
                              User.find(criteria3, function (err, getusers) {  // Third Query
                                  var len3 = getusers.length;
                                  for (var n = 0; n < len3; n++) {
                                      lunches[i].luckyUsers.push(getusers[n].name);
                                  }
                                  if (i == len - 1)
                                      callback(null, lunches); //this must be end of the ur loop
                              });
                          }
                      });
                  }
              })(i);
          }
      });
  }