我的猫鼬代码有问题。我正在尝试创建便笺,然后保存以将其发布。我也想将其保存到做笔记的用户的数组中。升级注释工作正常-问题是将其保存到用户配置文件时实现了相同的逻辑。如果ID是唯一的便笺,我想不出如何创建新便笺并替换现有便笺。请帮忙。
代码:
exports.post = async (username, note) => {
const user = await User.findOne({ username });
const newNote = await Note.findOneAndReplace(
{ title: note.title },
{
user: user._id,
title: note.title,
content: note.content
},
{ upsert: true,
new: true });
user.notes.addToSet(newNote); //NOT WORKING -- should only push unique notes to user's note array
await user.save();
return newNote; //returns note object to be saved in middleware function
};