【问题标题】:Mongoose findOneAndUpdate won't update my databaseMongoose findOneAndUpdate 不会更新我的数据库
【发布时间】:2020-07-11 03:25:11
【问题描述】:

我在开发应用程序时正在自学 NodeJS。我使用 Mongoose 和 Axios。

在应用程序的一个部分,我显示合作伙伴,当我点击更新时,我会得到一个包含所选合作伙伴信息的表格,以便更新他们。

客户端将信息发送到服务器,但服务器不会更新数据库中的条目。这是我的服务器端

app.post("/api/partner/update", (req, res) => {
const id = req.body.id;
const fullname = req.body.fullname;
const email = req.body.email;
const phones = req.body.phones;
const shops = req.body.shops;
const company = req.body.company;

console.log("The body is ",req.body) //It returns the correct data from the client's side submited data

Partner.findOneAndUpdate(id, mongoose.set('useFindAndModify', false),
 {
    "fullname": fullname,
    "email": email,
    "phones": phones,
    "shops":shops,
    "company": company
  },
  (err, document) => {
    if (err) return err;
    //console.log(document);
    res.send({ document });  

  }
);});

这是我的模型:

 const mongoose = require("mongoose");

const partnerSchema = mongoose.Schema(
  {
    fullname: {
      type: String,
      maxlength: 50,
      required: true
    },
    email: {
      type: String,
      trim: true,
      unique: 1,
      required: true
    },
    phones: {
      type: Array
    },
    company: {
      type: String,
      maxlength: 50,
      required: true
    },
    shops: {
      type: Array,
      default: 0
    },
    is_valid: {
      type: Boolean
    },
    validated_by: {
      type: String
    },

    created_by: {
      type: String,
      maxlength: 50
    },
    updated_by: {
      type: String
    },
    deleted_by: {
      type: String
    },
    deleted_at: {
      type: Date,
      default: null
    }
  },
  {
    timestamps: {
      createdAt: "created_at",
      updatedAt: "updated_at"
    }
  }
);

const Partner = mongoose.model("Partner", partnerSchema)
module.exports ={Partner}

我不明白为什么它不更新数据库中的字段

【问题讨论】:

    标签: node.js reactjs mongoose axios


    【解决方案1】:

    参考文档,https://mongoosejs.com/docs/tutorials/findoneandupdate.html findOneAndUpdate的第一个参数应该是一个过滤器对象,而不是直接的id。

    所以请尝试Partner.findOneAndUpdate({'_id': id},....

    【讨论】:

      【解决方案2】:

      根据文档,这是 findOneAndUpdate 的语法:

      var query = { name: 'borne' };
      Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)
      

      将数据库查询更改为:

      let query = {_id: id };
      let dataToUpdate= {
          "fullname": fullname,
          "email": email,
          "phones": phones,
          "shops":shops,
          "company": company
        }
      let options = {useFindAndModify: false}  // useFindAndModify set to false at query level
      // options = {useFindAndModify: false,new:true} if you want updated docs in return
      
      
       Partner.findOneAndUpdate(query,dataToUpdate,options,(err, document) => {
              if (err) return err;
              //console.log(document);
              res.send({ document });  
      
            }
          )
      

      在猫鼬级别使用选项设置弃用警告打开或关闭

      mongoose.set('useFindAndModify', false)
      

      在此处阅读更多信息:

      Deprecation Warnings

      findOneAndReplace

      你也可以使用findByIdAndUpdate方法:

      Model.findByIdAndUpdate(id, update, options, callback)
      

      您可以启用 Promise 以使代码更具可读性和可测试性: https://mongoosejs.com/docs/promises.html

      【讨论】:

        猜你喜欢
        • 2019-04-19
        • 1970-01-01
        • 2020-01-13
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        • 2019-03-14
        • 1970-01-01
        • 2020-05-29
        相关资源
        最近更新 更多