0
我有這樣的錯誤:貓鼬 - 蒙戈{[CastError:演員到對象失敗值「......」在路徑「...」
{ [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:......),但它仍然給我的錯誤
謝謝,我現在覺得很愚蠢,我太專注於路由代碼而不是模式 – xKedar