根據可用的數據,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: []
};
}
});
}
列表數組中的項是什麼? – abdulbarik
嗨abdulbarik,我編輯我的代碼 – klp
與添加res.send(resp);我得到錯誤resp沒有定義 – klp