Javascript 如何在NodeJS、Express中通过mongoose更新时设置动态密钥
因为有很多键我只想从一个函数更新。 我需要了解如何将密钥设置为动态更新Javascript 如何在NodeJS、Express中通过mongoose更新时设置动态密钥,javascript,node.js,typescript,express,Javascript,Node.js,Typescript,Express,因为有很多键我只想从一个函数更新。 我需要了解如何将密钥设置为动态更新 static async updateProfile(req, res, next) { const userId = req.body.userId; // I am receiving the key in body that needs to be updated const key = req.body.key; const value = req.body.value;
static async updateProfile(req, res, next) {
const userId = req.body.userId;
// I am receiving the key in body that needs to be updated
const key = req.body.key;
const value = req.body.value;
const toUpdate = {
userId: userId,
//here I want the key to be dynamic
key: value
};
try {
const updateNow = await Profile.findOneAndUpdate(
{ userId: userId },
toUpdate,
{ upsert: true }
);
} catch (err) {
console.log(err);
}
}
const toUpdate={
...
[键]:值
}
或
toUpdate[键]=值;
或
toUpdate[`${key}`]=值;
上述任何作品