Promise.all(membersArray.map((member) => {
return db
.doneTodo
.find({'victor._id': member._id})
.then((userVictories) => {
return {
email: member.email,
victories: userVictories.length
};
});
})).then((memberStats) => {
console.log(membersStats);
});
db.doneTodo
是一個Mongoose模式。我有一個問題:瞭解Db/Map的返回語句JS
問:爲什麼我們需要「db.doneTodo ...」旁邊的「return」語句?爲什麼第二個「回報」不夠?
我的理由:由於我們正在申請地圖的每個元素,我們正在申請此功能:
{
return db
.doneTodo
.find({'victor._id': member._id})
.then((userVictories) => {
return {
email: member.email,
victories: userVictories.length
}
});
}
而
.then((userVictories) => {
return {
email: member.email,
victories: userVictories.length
}
則計算結果爲
{
email: member.email,
victories: userVictories.length
}
也是如此:
db
.doneTodo
.find({'victor._id': member._id})
.then((userVictories) => {
return {
email: member.email,
victories: userVictories.length
}
});
}
評估,以
{
email: member.email,
victories: userVictories.length
}
因此,我們回到了嗎?