我嘗試將屬性添加到對象。我有一個與客戶的名單,併爲每個客戶我想添加一個數組與郵件發送給該客戶。如何通過循環更新對象項目
但我不能將它添加到現有的對象。我做錯了什麼?
crmuser.find().exec(function(err, crmusers){
console.log(crmusers);
//LOG result
[ { _id: 59563a7181438f4db8193379,
emailName: 'Donald Duck',
shop: 'dd',
moreproperties: '',
email: '[email protected]',
} ]
async.each(Object.keys(crmusers), function(key, callback){
mailService.getLogs({to: crmusers[key].email, typeMail: "CRM"}, function(err, result){
console.log("res", result); // here we have the result from the mailService.getLogs() function
crmusers[key]["sendMail"] = {result}; //Here I try to add a new property to the object
console.log("USERR", crmusers[key]); // And here I can see that the property is not added
callback();
})
}, function(){
status = 200;
response = {message: 'OK', crmusers};
return res.status(status).json(response);
})
})
您可以登錄'err'並檢查是否有錯誤? – Sridhar
你的代碼適合我。可能多個元素在數組中相同的ID? – Sridhar