我有一個包含用戶 ID 的陣列
像這樣的陣列 ["userid1","userid2","userid3"]
這個陣列可以有許多用戶的 id 或只有一個。只是隨機的。
我只是想更新用戶的資料
如果陣列有 5 個用戶的 id,它應該更新 5 個用戶的資料
這就是我所做的
const { mentionUsers, sender, notificationType, image } = req.body;
console.log(mentionUsers, sender, notificationType, image);
try {
if (mentionUsers.length === 0)
return res.status(204).json({ message: "no mention users" });
mentionUsers.map(async (userId) => {
let notification = {
_id: new mongoose.Types.ObjectId(),
sender,
notificationType,
image,
read: false,
};
let user = await User.findByIdAndUpdate(
userId,
{ $push: { notifications: notification } },
{ new: true }
);
console.log(user);
});
它不會更新用戶的資料
如何更新資料?感謝您閱讀我的問題
uj5u.com熱心網友回復:
您可以一次更新多個檔案。
let notification = {
_id: new mongoose.Types.ObjectId(),
sender,
notificationType,
image,
read: false
};
User.update(
{ _id: { $in: mentionUsers } },
{ $push: { notifications: notification } },
{ new: true }
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/504013.html
