我想顯示一個帶有附件的頁面,以及訂閱此附件的所有電子郵件。但是郵件顯示不正確。有時會在該欄位中插入一封電子郵件,該欄位應位于下一個欄位中。當應該有一封電子郵件時,該欄位仍然為空(該電子郵件在另一個欄位中)。然后我決定用 for await 替換 forEach。但情況并沒有改變。有什么問題?
router.get('/', auth, async (req, res) => {
try {
await Attachment.find(async (err, attachments) => {
let html_table = ""
for await (const attachment of attachments) {
html_table = "<tr>"
html_table = "<td>"
html_table = attachment.file_name
html_table = "</td>"
html_table = "<td>"
console.log(attachment.id)
await User.find({
attachments: attachment.id
}, async (err, users) => {
for await (const user of users){
html_table = user.email "<br />"
console.log(user.email)
}
})
html_table = "</td>"
html_table = "</tr>"
}
res.json(html_table)
})
} catch (e) {
res.status(500).json({ message: e.message })
}
})
uj5u.com熱心網友回復:
您將承諾與回呼混合在一起 -res.json將在內部回呼User.find完成之前執行。您可能正在尋找類似的東西:
router.get('/', auth, async (req, res) => {
try {
const attachments = await Attachment.find();
let html_table = ""
for (const attachment of attachments) {
html_table = "<tr>"
html_table = "<td>"
html_table = attachment.file_name
html_table = "</td>"
html_table = "<td>"
console.log(attachment.id)
const users = await User.find({
attachments: attachment.id
});
for (const user of users){
html_table = user.email "<br />"
console.log(user.email)
}
html_table = "</td>"
html_table = "</tr>"
}
res.json(html_table);
} catch (e) {
res.status(500).json({ message: e.message })
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367578.html
