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

Posted

技术标签:

【中文标题】当我运行 POST 请求时,为啥它不返回我的 Mongoose 模式定义中定义的 JSON?【英文标题】:Why doesn't it return the JSON as defined in my Mongoose schema definition when I run the POST request?当我运行 POST 请求时,为什么它不返回我的 Mongoose 模式定义中定义的 JSON? 【发布时间】:2015-11-19 22:50:23 【问题描述】:

我在 Postman 上运行 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);

邮递员

【问题讨论】:

state添加到回调以保存.save(function(err,state) 。这样您就可以确定该对象是完成所有验证和转换后返回的对象。您还应该能够传入正文而不是分别分配每个属性,即var state = new Rankings(req.body); 实际上这正是您的问题。与state.state = req.body.state 相比,查看workforce = req.body.workforce 中的任何over 字段如何分配给state。它们不存在是因为您从未分配过它们。 谢谢布莱克。这很有意义:) 【参考方案1】:

在您的控制器

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.,因此您只是在分配变量。 但我不知道如果不使用这是否真的将您的数据添加到数据库中:

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)) 以确保返回的是好的数据

【讨论】:

非常感谢您的帮助!

以上是关于当我运行 POST 请求时,为啥它不返回我的 Mongoose 模式定义中定义的 JSON?的主要内容,如果未能解决你的问题,请参考以下文章

为啥nodejs express服务器在我使用post时返回302?

为啥我的 post 请求返回 405 错误? [复制]

为啥我的 Google Maps API 请求在服务器上运行时不起作用

为啥我的 POST 请求在部署时被 CORS 阻止但它在本地工作

为啥我的 formdata POST 没有被 Cors 政策阻止?

为啥我的 PATCH 请求的响应是空的? (Javascript)