[英]How to set dynamic key while updating via mongoose in NodeJS, Express
Since there are a lot of keys that I want to update from just one function.由于我只想从一个 function 更新很多密钥。 I need to understand how I can set the key to update dynamically.
我需要了解如何将密钥设置为动态更新。
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 = {... [key]: value } or toUpdate[key] = value; or toUpdate[`${key}`] = value; any of the above works
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.