【发布时间】: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