0
我需要連接mongoose查詢結果到單個JSON對象。 問題是我傳遞一個數組到貓鼬即;如何在for循環中連接Mongoose查詢結果?
Modem Serial: [11111111111,nodata,3333333333333333]
我需要的是連接這樣的數據;
Final Modem : [{"m_model":"Modem 1","m_serial_no":"11111111111","available":"dispatched"},
{"m_model":"No data","m_serial_no":"No data"},
{"m_model":"Modem3","m_serial_no":"3333333333333333","available":"dispatched"}]
這是我的代碼;
for(i=0;i<modem_serial.length;i++){
console.log("Modem Serial: "+modem_serial[i]);
Modem.findOne({m_serial_no: modem_serial[i]},{_id: 0,__v:0},function (err,m_data) {
//console.log("err: "+err);
if(m_data!=null){
modem=modem.concat(m_data);
}else{
console.log("\n\n\nm_data : "+m_data);
modem=modem.concat({
m_model: 'No data',
m_serial_no: 'No data'
});
}
console.log("\n\n\nFinal Modem : "+JSON.stringify(modem));
});
}
但刷新後我得到這個,
最終調制解調器:
[{"m_model":"No data","m_serial_no":"No data"},{"m_model":"Modem 1","m_serial_no":"11111111111","available":"dispatched"},{"m_model":"Modem 3","m_serial_no":"3333333333333333","available":"dispatched"}]
the `No data` is going to first index. why??
這裏是我的完整代碼,
router.get('/', function(req, res, next) {
Dispatched.find({status:"installed"},function(err,dispatched_data) {
//console.log("SUCCES data: "+dispatched_data);
var client_name=[],branch_name=[],serial=[],data_card_serial=[],sim_number=[],modem_serial=[],idu_serial=[],installed_date=[],
ip_address=[],notes=[],ir_report=[],date,ipp={},ip=[],ir,note;
for (var j = 0; j < dispatched_data.length; j++) {
client_name=client_name.concat([{client_name:dispatched_data[j].client_name}]);
branch_name.push(dispatched_data[j].branch_name);
serial.push(dispatched_data[j].router_serial);
data_card_serial.push(dispatched_data[j].data_card_serial);
sim_number.push(dispatched_data[j].sim_number);
if(dispatched_data[j].modem_serial != null){
modem_serial.push(dispatched_data[j].modem_serial);
}else{
modem_serial.push("nodata");
}
idu_serial.push(dispatched_data[j].idu_serial);
ip_address.push({ip_address:dispatched_data[j].ip_address});
installed_date.push({installed_date:dispatched_data[j].installation_date});
notes.push({notes:dispatched_data[j].notes});
ir_report.push({ir_report:dispatched_data[j].ir_report});
}
var data=[],router=[],datacard=[],sim=[],modem=[],idu=[],branch=[],i;
console.log("Modem Serial: "+modem_serial.toString());
console.log("\n\n\nDispatched Data Length ="+dispatched_data.length);
for(i=0;i<modem_serial.length;i++){
console.log("Modem Serial: "+modem_serial[i]);
Modem.findOne({m_serial_no: modem_serial[i]},{_id: 0,__v:0},function (err,m_data) {
//console.log("err: "+err);
if(m_data!=null){
modem=modem.concat(m_data);
}else{
console.log("\n\n\nm_data : "+m_data);
modem=modem.concat({
m_model: 'No data',
m_serial_no: 'No data'
});
}
console.log("\n\n\nFinal Modem : "+JSON.stringify(modem));
});
}
res.end();
});
});
非常感謝!有效!我早些時候嘗試了異步,但我沒有得到正確的結果。我嘗試了瀑布和串行異步。但我不明白這一點。 – AnonymousObject
hello'Mickey', 我有一個疑問,我有像modem_serial和多個結果像'調制解調器'多個數組。如何將所有結果連接到一個json數組。我希望它在數據表中顯示它。 – AnonymousObject
@AnonymousObject發佈另一個問題,包括您開始的一個示例以及您想要結束的內容。 – Mikey