我有一个问题,当我使用mongoose "findOneAndReplace "更新数据时,我收到一个验证模式错误,特别是似乎字段是空的。
在相同的数据下,我没有问题,其他的粗暴操作,如 "创建 "和 "删除",所以绝对不是与模式或数据有关。
反正这里是我的代码。
服务在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;
这可能是什么?
不要使用 $set
与Mongoose一起使用(这是Mongo的本地操作符)。第二个参数必须是一个包含要更新字段的对象 (文档). 不要加 $set
. 蒙哥斯认为 $set
是更新的关键,所以没有通过你的验证。
Clienti.findOneAndReplace(
{_id:req.params.id},
{
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
},