如何在 NodeJS、Express 中通过猫鼬更新时设置动态密钥

时间:2021-05-02 19:02:42

标签: 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;

    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);
    }
}

1 个答案:

答案 0 :(得分:1)

axios.get('route', {
  headers: {
    Authorization: localStorage.getItem('jwt'),
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache'
  }
})
  .then(res => {
    // do something
  });

相关问题