model.js:
const mongoose = require('mongoose');
const Schema =mongoose.Schema;
const CountryInfoSchema = new Schema({
country_code:{type:String},
country_name:{type:String},
is_active:{type:Boolean},
created_on:{ type: Date, default: Date.now },
created_by:{type:String},
updated_on:{ type: Date, default: Date.now },
updated_by:{type:String}
});
module.exports =mongoose.model('country',CountryInfoSchema);
Controller.js:
const countryModel= require("./../userDetailsModels/country-info");
var createCountryDetails =(req,res) =>{
var countryinfo = new countryModel(req.body);
countryinfo.save()
.then(info => {
res.send({
country_code:req.body.country_code,
country_name:req.body.country_name,
is_active:req.body.is_active,
created_by:req.body.created_by,
updated_by:req.body.updated_by,
created_on:info.created_on,
updated_on:info.updated_on,
id:info._id
});
})
.catch(err => {
res.status(400).send({
message:err
});
});
}
module.exports ={
createCountryDetails: createCountryDetails,
}
在邮递员中,我测试了api
要求正文:
{
"country_code":"IND",
"country_name":"India",
" is_active":true,
"created_by":"SA",
"updated_by":"SA"
}
像这样在下面获取输出
{
"created_on": "2018-08-28T06:51:57.278Z",
"updated_on": "2018-08-28T06:51:57.278Z",
"id": "5b84f10d7ca3bf2654d6af39"
}
其他字段country_code显示未定义。我不知道哪里出了错误。调试时我得到所有字段都是未定义。我正在使用mongo db。在无法使用api创建数据之前,此代码已正常工作
我需要如下输出:
{
"country_code":"IND",
"country_name":"India",
" is_active":true,
"created_by":"SA",
"updated_by":"SA",
"created_on": "2018-08-28T06:51:57.278Z",
"updated_on": "2018-08-28T06:51:57.278Z",
"id": "5b84f10d7ca3bf2654d6af39"
}