嗨,我在节点js中有一个如下的架构。
var mongoose = require('mongoose');
const ContactSchema = mongoose.Schema({
first_name:{
type:String,
//required:true
},
last_name:{
type:String,
//required:true
},
phone:{
type:String,
//required:true
}
});
const Contact = module.exports = mongoose.model('Contact',ContactSchema);
现在我正在尝试使用邮递员从router.js中保存对象。
//router.js
const Contact = require('../models/contacts');
//Create a contact list node server (mongodb)
router.post('/contact',(req,res,next)=>{
var newContact = new Contact({
first_name:req.body.first_name,
last_name:req.body.last_name,
phone:req.body.phone
});
//Save contact list and add call back as well
newContact.save((err,contact)=>{
if (err){
res.json({msg:'Failed to add contact list'});
} else {
res.json(contact);
}
});
});
并以邮递员身份传递json。
{
"first_name": "Sunil",
"last_name": "fdgf",
"phone": "8557988004"
}
因此,它仅将ID保存在mongo db中,而不保存其他字段。您能帮我解决这里的问题吗?
感谢前进