簡體   English   中英

ZCCADDEDB567ABAE643E15DCF0974E503Z 更新 function 正在提供 null 而不是實際更新信息

[英]Mongoose update function is giving null and not actually updating information

My.findOneAndUpdate 方法將用戶返回為 null 並且不會最終更新信息。 一切似乎都井井有條,我沒有任何錯誤。

編輯:我取得了進展,我終於能夠更新 GroupID,但將其設置為 null。 而不是傳入的字符串。

router.put("/update", (req, res) => {
  Users.findOneAndUpdate(
    { _id: req.body.id },
    {
      $set: { GroupID: req.body.GroupID }
    },
    { new: true },
    (err, user) => {
      if (err) res.send(err);
      else res.send("Account GroupID Updated" + user);
    }
  );
});

您必須將req.body.id轉換為 objectId,如下所示:

var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId(req.body.id);

 Users.findOneAndUpdate(
    { _id: id }

暫無
暫無

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

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