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

编程入门 行业动态 更新时间:2024-10-27 22:18:57

猫鼬findOneAndUpdate不会更新我的<a href=https://www.elefans.com/category/jswz/34/1771350.html style=数据库"/>

猫鼬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不会更新我的数据库

本文发布于:2024-05-07 03:18:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   猫鼬   findOneAndUpdate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!