为什么当我运行POST请求时,它不会返回我在Mongoose模式定义中定义的JSON?

我有问题在邮递员上运行POST请求。 当我运行一个POST请求时,它只返回在我的模型中定义的模式的一部分。 我想在我的Mongoose模式定义中定义完整的JSON。 这里是代码:

模型:

'use strict'; //******************* //Dependencies //******************* var mongoose = require('mongoose'); var Schema = mongoose.Schema; //attach Schema method on the mongoose object //Create a new Schema var rankingsSchema = new mongoose.Schema ({ overall: Number, state: String, workforce: Number, costOfDoingBusiness: Number, infrastructure: Number, economy: Number, qualityOfLife: Number, technologyAndInnovation: Number, education: Number, business: Number, costOfLiving: Number, accessToCapital: Number }); //Export the Schema model module.exports = mongoose.model('Rankings', rankingsSchema); 

调节器

 var Rankings = require('../models/Rankings'); module.exports = { //Create endpoint /api/states for POST post: function(req, res) { //Create a new instance of the State model var state = new Rankings(); //Set the state properties from POST state.overall = req.body.overall; state.state = req.body.state; workforce = req.body.workforce; costOfDoingBusiness = req.body.costOfDoingBusiness; infrastructure = req.body.infrastructure; economy = req.body.economy; qualityOfLife = req.body.qualityOfLife; technologyAndInnovation = req.body.technologyAndInnovation; education = req.body.education; business = req.body.business; costOfLiving = req.body.costOfLiving; accessToCapital = req.body.accessToCapital; //Save the data and check for errors state.save(function(err) { if(err) res.send(err); res.json ({message: "State Added", data: state}); }); }, 

服务器

 //******************* //Start Endpoints //******************* router.route('/state') .post(RankingsController.post) .get(RankingsController.get); 

邮差

在这里输入图像说明

在你的控制器

 var Rankings = require('../models/Rankings'); module.exports = { //Create endpoint /api/states for POST post: function(req, res) { //Create a new instance of the State model var state = new Rankings(); //Set the state properties from POST state.overall = req.body.overall; state.state = req.body.state; state.workforce = req.body.workforce; state.costOfDoingBusiness = req.body.costOfDoingBusiness; state.infrastructure = req.body.infrastructure; state.economy = req.body.economy; state.qualityOfLife = req.body.qualityOfLife; state.technologyAndInnovation = req.body.technologyAndInnovation; state.education = req.body.education; state.business = req.body.business; state.costOfLiving = req.body.costOfLiving; state.accessToCapital = req.body.accessToCapital; //Save the data and check for errors state.save(function(err) { if(err) res.send(err); res.json ({message: "State Added", data: state}); }); }, 

你失踪的state. 在每个字段之前你想添加数据库,所以你只是分配variables。 但是我不知道这是否真的在数据库中添加数据,如果不使用:

 var Rankings = require('../models/Rankings'); module.exports = { //Create endpoint /api/states for POST post: function(req, res) { //Create a new instance of the State model var state = new Rankings({ overall: req.body.overall, state: req.body.state, workforce: req.body.workforce, costOfDoingBusiness: req.body.costOfDoingBusiness, infrastructure: req.body.infrastructure, economy: req.body.economy, qualityOfLife: req.body.qualityOfLife, technologyAndInnovation: req.body.technologyAndInnovation, education: req.body.education, business: req.body.business, costOfLiving: req.body.costOfLiving, accessToCapital: req.body.accessToCapital }); //Save the data and check for errors state.save(function(err, state) { if(err) res.send(err); res.json ({message: "State Added", data: state}); }); }, 

正如@Blakes Seven所说的,最好将state添加到.save(function(err)) ,以确保返回的数据是正确的