簡體   English   中英

使用 mongoose 在動態路由下將新數據插入 mongodb

[英]insert new data into mongodb under dynamic route using mongoose

我有一個為特定用戶添加附加信息的表單,我的架構有名稱、密碼、email,並且在用戶首先創建帳戶之前不會詢問教育和教育數據。 用戶注冊后,它會將他們路由到類似“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 插入數據時,我該怎么辦?

嘗試將{ userid },更改為{_id: userid }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM