【发布时间】:2017-01-03 03:05:11
【问题描述】:
我有以下 Mongoose 架构。
var ContactSchema = new Schema({
name: { type: String, required: true },
company: { type: String, required: true },
email: { type: String, required: true },
phone: { type: String, required: true }
},{ versionKey: false });
当我创建联系人帖子时,通过
var contact = new Model(req.body);
contact.save( function( err, result ) {
if( err ) {
res.status(422).json( err );
}
else {
res.status(200).json( result );
}
})
并且不提交所有字段,我收到错误 422。 required: true 有效。
但是,当我通过
更新现有联系人时Model.findByIdAndUpdate(req.params.id, req.body, {new:true, select: defaultProjection}, function( err, contact ){
if( err ) {
res.status(422).json( err );
}
else {
res.status(200).json( contact );
}
});
如果我离开,我不会收到任何错误,例如,email 为空。
我做错了什么? :-)
【问题讨论】: