简体   繁体   English

Node js Rest Service 将数据保存到特定对象json

[英]Node js Rest Service save data into specific object json

I'm developing a REST API with Node JS.我正在使用 Node JS 开发 REST API。 I have developed post request but don't try to save data on a specific object of MongoDB Schema.我已经开发了发布请求,但不要尝试在 MongoDB Schema 的特定对象上保存数据。 I have this MongoDB schema我有这个 MongoDB 模式

var ProfileSchema = db.Schema({
farmId: { type: String, required: true },
companyName: { type: String, required: true },
firstname: {type: String, required: true},
surname: {type: String, required: true},
address: {type: String, required: true},
city: {type: String, required: true},
state: {type: String, required: false},
postalcode: {type: String, required: true},
country: {type: String, required: true},
telephone: {type: String, required: true},
email: {type: String, required: true},
demographics:{
farmSize: {type: String, required: true },
crops: {type: String, required: true},
liveStock: {type: String, required: true},
precisionFarming: {type: String, required: true},
currentTire: {type: String, required: false}
}
});

and this my post request:这是我的帖子请求:

router.post('/profile', VerifyToken, function (req, res) {
Profile.create({
    farmId:req.body.farmId,
    companyName:req.body.companyName,
    firstname:req.body.firstname,
    surname: req.body.surname,
    address: req.body.address,
    city: req.body.city,
    state: req.body.state,
    postalcode: req.body.postalcode,
    country: req.body.country,
    telephone: req.body.telephone,
    email: req.body.email,
    farmSize: req.body.farmSize,
    crops: req.body.crops,
    liveStock: req.body.liveStock,
    precisionFarming: req.body.precisionFarming,
    currentTire: req.body.currentTire
    },
    function (err, profile) {
        if (err) return res.status(500).send("There was a problem adding the information to the database.");
        res.status(200).send({status: 'ok', data: { msg: 'Profile saved'}});

    });
  });

How can i save the data into "demographics" object?如何将数据保存到“人口统计”对象中? My code give erro 500. Any help please, is very importat for me to fix this problem.我的代码给出错误 500。请提供任何帮助,这对我解决此问题非常重要。

Best最好的事物

This error log:此错误日志:

{ ValidationError: ProfileFarm validation failed:    demographics.precisionFarming: Path `demographics.precisionFarming` is     required., demographics.liveStock: Path `demographics.liveS
tock` is required., demographics.crops: Path `demographics.crops` is required., demographics.farmSize: Path `demographics.farmSize` is required.

Can you log your req.body ?你能记录你的req.body吗?

Your schema definition has few fields marked as required and I am afraid you are not passing to them or they are going undefined, eg: crop, liveStock, etc Here's what you can do:您的架构定义几乎没有标记为必需的字段,恐怕您没有传递给它们,或者它们未定义,例如:crop、liveStock 等这是您可以执行的操作:

  • Send all the data properly正确发送所有数据

Or或者

  • just change the schema definition and remove the required flags, or set them to false it would start working.只需更改架构定义并删除所需的标志,或将它们设置为 false 即可开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM