2016-09-20 54 views
1

根據可用的數據,ui應該得到2個reults,但只有一個,因爲我把res.send放到了循環中,所以它會結束,任何人都可以幫助我...... ..如何在node js中執行forloop

exports.getrequestsdetails = function(req, res) { 
    var params = req.params; 
    console.log(params) 
    var record = db.collection('requests'); 
    var item = { 
     "sent_id": params.id, 
     "status": 1 
    } 
    record.find(item).toArray((err, result) => { 
     if (err) { 
      return 
     } 
     if (result) { 
      for (var i in result) { 
       var id = result[i].recieved_id; 
       var profile = db.collection('profile'); 
       profile.find({ 
        '_id': new ObjectId(id) 
       }).toArray((err, resp) => { 
        if (err) { 
         return 
        } 
        if (resp) { 
         console.log(resp); 
        } else {} 
       }); 
      } 
      res.send(resp); 
     } //end of if loop 
     else { 
      response = { 
       status: 'fail', 
       data: [] 
      }; 
     } 

    }); 

} 
+0

列表數組中的項是什麼? – abdulbarik

+0

嗨abdulbarik,我編輯我的代碼 – klp

+0

與添加res.send(resp);我得到錯誤resp沒有定義 – klp

回答

1

問題在於獲取配置文件。您正在使用mongodb的查找,它是異步。因此,在您的for週期中,您將開始提取配置文件,但是在完成配置文件的提取之前,您就發送了好的res.send

profile.find(...的回撥將在res.send後執行。除此之外,resp變量位於find回調的內部,您正試圖在res.send之外。

要處理此問題,無論是使用異步還是承諾。請參閱下面使用promise的代碼。

var Promise = require('bluebird') 

exports.getrequestsdetails = function(req, res) { 
    var params = req.params; 
    console.log(params) 
    var record = db.collection('requests'); 
    var item = { 
     "sent_id": params.id, 
     "status": 1 
    } 

    record.find(item).toArray((err, result) => { 
     if (err) { 
      return 
     } 
     if (result) { 

      var profiles_to_get = [] 
      var profiles = [] 

      for (var i in result) { 
       var id = result[i].recieved_id; 
       profiles_to_get.push(get_profile(id, profiles)) 
      } 

      Promise.all(profiles_to_get) 
       .then(() => { 
        res.send(profiles); 
       }) 
     } //end of if loop 
     else { 
      response = { 
       status: 'fail', 
       data: [] 
      }; 
     } 

    }); 

    function get_profile (id, profiles) { 
     return new Promise(function (resolve, reject) { 
      var profile = db.collection('profile'); 
       profile.find({ 
        '_id': new ObjectId(id) 
       }).toArray((err, resp) => { 
        if (err) { 
         reject(err) 
         return 
        } 
        if (resp) { 
         profiles.push(resp) 
         resolve() 
        } else { 
         reject() 
        } 
       }); 
     }) 
    } 

} 

這是如何工作的,它創建個人資料,找到並將其存儲在var profiles_to_get = []的列表。你使用Promise.All(profiles_to_get)這將讓你在所有的配置文件被提取後做的東西。

+0

我得到錯誤意外的令牌) – klp

+0

它告訴你是行號? –

+0

這是固定的,它說諾言。所有不是一個功能 – klp

1

您只能發送一個響應回請求。

在for循環外定義一個變量,向它追加記錄,然後在for循環結束後發送它。

exports.getrequestsdetails = function(req, res) { 
var params = req.params; 
console.log(params) 
var record = db.collection('requests'); 
var item = { 
    "sent_id": params.id, 
    "status": 1 
} 

var resList = []; 

record.find(item).toArray((err, result) => { 
     if (err) { 
      return 
     } 
     if (result) { 
      for (var i in result) { 
       var id = result[i].recieved_id; 
       var profile = db.collection('profile'); 
       profile.find({ 
        '_id': new ObjectId(id) 
       }).toArray((err, resp) => { 
         if (err) { 
          return 
         } 
         if (resp) { 
          console.log(resp); 
          resList[i] = resp; 
         } 
         else{ 
         } 
       }); 
      } 
     }//end of if loop 
     else { 
      resList = { 
       status: 'fail', 
       data: [] 
       }; 
     } 
     res.send(resList); 
     }); 
+0

ca請發帖一些代碼 – klp

+0

編輯添加代碼。 –

+0

它不會去工作,你怎麼能''我'價值超出它的範圍 – abdulbarik

0

可以循環

1

您可以pushresplist陣列畢竟在一個陣列添加所有的名單和最後發送的數據,並在完成後loop發送。

像這樣:

exports.getrequestsdetails = function(req, res) { 
    var params = req.params; 
    console.log(params); 

    var record = db.collection('requests'); 
    var item = { 
     "sent_id": params.id, 
     "status": 1 
    }; 

    record.find(item).toArray((err, result) => { 
     if (err) { 
      return err; 
     } 
     if (result) { 
      var list = []; 
      for (var i in result) { 
       var id = result[i].recieved_id; 
       var profile = db.collection('profile'); 
       profile.find({ 
        '_id': new ObjectId(id) 
       }).toArray((err, resp) => { 
        if (err) { 
         return err; 
        } 
        else{ 
         list.push(resp); 
         console.log(resp); 
         if(i===result[result.length-1]){ 
          res.send(list); 
         } 
        } 
       }); 
      } 

     } //end of if loop 
     else { 
      response = { 
       status: 'fail', 
       data: [] 
      }; 
     } 

    }); 
}; 

希望這對你的工作

+0

但我應該在哪裏保持res.send(resp)。? – klp

+0

當你想發送資源時,這取決於你嗎? – abdulbarik

+0

現在它完成for循環後發送 – abdulbarik

1

不要在異步模式下循環使用。而不是像下面那樣使用異步模塊。

var async = require('async'); 
exports.getrequestsdetails = function (req, res) { 
    var params = req.params; 
    console.log(params) 
    var record = db.collection('requests'); 
    var item = { 
     "sent_id": params.id, 
     "status": 1 
    } 
    record.find(item).toArray(function (err, result) { 
     if (err) { 
      return 
     } 
     if (result) { 
      var list = []; 
      async.each(result, function (item, cb) { 
       var id = item.recieved_id; 
       var profile = db.collection('profile'); 
       profile.findOne({ 
        '_id': new ObjectId(id) 
       }, function (err, resp) { 
        if (err) { 
         return cb(); 
        } 
        if (resp) { 
         list.push(resp); 
         console.log(resp); 
         return cb(); 
        } 
        return cb(); 
       }); 
      }, function (err) { 
       res.send(list); 
      }); 
     }//end of if loop 
     else { 
      response = { 
       status: 'fail', 
       data: [] 
      }; 
     } 

    }); 

}