【发布时间】:2017-03-23 22:41:05
【问题描述】:
我遇到了这个错误:
{ [ValidationError: Contact validation failed]
message: 'Contact validation failed',
name: 'ValidationError',
errors:
{ Contact:
{ [CastError: Cast to Object failed for value "ContactExample" at path "Contact"]
message: 'Cast to Object failed for value "ContactExample" at path "Contact"',
name: 'CastError',
kind: 'Object',
value: 'ContactExample',
path: 'Contact',
reason: undefined },
}
Same for ContactType
我的代码如下:
ContactSchema.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Contact = new Schema({
Contact: {Type: String},
ContactType: {Type: String},
ContactOwner: {type: String}
});
module.exports = mongoose.model("Contact", Contact);
路线:
app.post('/newcontact', isLoggedIn, function(req, res) {
ContactModel.count({"Contact": req.body.Contact}, function(err, result){
if (result>0) res.send('Contact already taken');
else{
var newContact = new ContactModel();
console.log(req.body);
newContact.ContactOwner = req.user.email;
newContact.Contact = req.body.Contact;
newContact.ContactType = req.body.ContactType;
newContact.save(function(err){console.log(err);});
res.send('Done');
}
});
});
我尝试填写“_id”字段并执行 ContactModel(ContactOwner: ......) 但它仍然给我这个错误
【问题讨论】: