我有post方法,当我这样做时工作正常
var companyDetailsModel = mongoose.model('companyDetails')
router.post('/companyDetails', function(req, res, next) {
var newCompanyDetails = new companyDetailsModel();
newCompanyDetails.companyName=req.body.companyName;
newCompanyDetails.streetAddress=req.body.streetAddress;
newCompanyDetails.city=req.body.city;
newCompanyDetails.Street=req.body.Street;
newCompanyDetails.ZipCode=req.body.ZipCode;
newCompanyDetails.Phone=req.body.Phone;
newCompanyDetails.Fax=req.body.Fax;
newCompanyDetails.webSite=req.body.webSite;
newCompanyDetails.Logo=req.body.Logo;
newCompanyDetails.save(function(err) {
if (err){
console.log('Error in Saving user: '+err);
}
console.log('User Registration succesful');
res.send("user added sucessfully");
});
})
这条路线工作正常,数据保存在数据库中,但如果我尝试这样做,如下所示
router.post('/companyDetails', function(req, res, next) {
var newCompanyDetails = new companyDetailsModel();
newCompanyDetails= req.body;
newCompanyDetails.save(function(err) {
if (err){
console.log('Error in Saving user: '+err);
}
console.log('User Registration succesful');
res.send("user added sucessfully");
});
})
我会得到内部服务器错误,如newCompanyDetails.save不是一个函数请说明为什么这个行为,如果一个帖子请求包含100个字段应该我明确应该我分配所有字段请帮我理解这个
答案 0 :(得分:1)
用这行删除mongo对象
newCompanyDetails= req.body;
请改为:
var newCompanyDetails = new companyDetailsModel(req.body);