【问题标题】:insert new data into mongodb under dynamic route using mongoose使用 mongoose 在动态路由下将新数据插入 mongodb
【发布时间】:2020-04-27 02:01:37
【问题描述】:

我有一个为特定用户添加附加信息的表单,我的架构包含姓名、密码、电子邮件和教育,并且在用户首先创建帐户之前不会询问教育数据。用户注册后,它会将他们路由到“localhost:5000/user/setting/5c27381923csw123923”(最后一部分是用户 ID)。现在我想将教育数据添加到我的 mongodb 中的用户集合中。

form class="info" action="/users/setting" method="POST">
        <div class="fields">
            <div class="education">
                <label for="education">Education</label>
                <select name="education" id="education">
                    <option value="bachelor">bachelor</option>
                    <option value="Master">Master</option>
                    <option value="Phd">Phd</option>
                    <option value="Other">Other</option>
                </select>
            </div>
        </div>

        <button type="submit" class="signin-button">Submit</button>

    </form>

我的发帖路线如下所示:

router.post("/setting/:id", (req, res, next) => {
  const userid = req.params.id;
  const { education } = req.body;
  User.updateOne(
    { userid },
    { $set: { education } },
    (err) => {
      if (err) {
        throw err;
      } else {
        res.send("good");
      }
    }
  );
});

当我单击“提交”按钮时,没有任何内容被更改或插入。当路由是动态的,如“localhost:3000/user/setting/5c2312327318273s”并尝试为该特定用户ID插入数据时,我该怎么办?

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    尝试将{ userid }, 更改为{_id: userid }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 2015-10-11
      • 2014-10-06
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      相关资源
      最近更新 更多