最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

猫鼬findOneAndUpdate不会更新我的数据库

IT培训 admin 13浏览 0评论

猫鼬findOneAndUpdate不会更新我的数据库

我在开发应用程序时正在自学NodeJS。我使用猫鼬和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.bodypany;

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}

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

回答如下:

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

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

猫鼬findOneAndUpdate不会更新我的数据库

我在开发应用程序时正在自学NodeJS。我使用猫鼬和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.bodypany;

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}

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

回答如下:

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

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

发布评论

评论列表 (0)

  1. 暂无评论