【发布时间】:2022-01-09 13:26:14
【问题描述】:
当我使用猫鼬“findOneAndReplace”更新数据时遇到问题, 我收到验证架构错误, 特别是这些字段似乎是空的。
使用相同的数据,我对“创建”和“删除”等其他 crud 操作没有问题, 所以绝对与架构或数据无关
无论如何这里是我的代码:
Angular 中的服务
updateCustomer(customer){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.put(this.UpdateCustomer+'/'+customer._id, JSON.stringify(customer), {headers: headers})
.map((response: Response) => response.json())
}
猫鼬放
app.put('/api/aggiorna_cliente/:id', function(req, res, next) {
console.log(JSON.stringify(req.body)) --> IS POPULATED
Clienti.findOneAndReplace(
{_id:req.params.id},
{$set:{
address:req.body.address,
brand:req.body.brand,
cap:req.body.cap,
city:req.body.city,
civico:req.body.civico,
email:req.body.email,
fiscalcode:req.body.fiscalcode,
provincia:req.body.provincia,
utente:req.body.utente
}
},
function (err, post) {
if (err) return next(err);
res.json(post);
});
});
客户端架构
const mongoose = require('mongoose');
const clientiSchema = mongoose.Schema({
utente:{
type: String,
required:true
},
cap:{
type: Number,
required:true
},
civico:{
type: String,
required:true
},
city:{
type: String,
required:true
},
address:{
type: String,
required:true
},
fiscalcode:{
type:String,
required:true
},
email:{
type:String,
required:true
},
brand:{
type:String,
required:true
},
provincia:{
province: String,
sigle: String
}
});
const Clienti = mongoose.model('Clienti',clientiSchema);
module.exports = Clienti;
会是什么?
【问题讨论】:
标签: node.js mongoose mongoose-schema