req.body在帖子上是空的

时间:2018-03-04 16:37:22

标签: node.js mongodb express post postman

我一直在搜索网站一段时间没有运气。我有:

module.exports.locationsCreate = function (req, res) {
  console.log(req.body);
  Loc.create({
    name: req.body.name,
    address: req.body.address,
    facilities: req.body.facilities.split(","),
    coords: [parseFloat(req.body.lng), parseFloat(req.body.lat)],
    openingTimes: [{
      days: req.body.days1,
      opening: req.body.opening1,
      closing: req.body.closing1,
      closed: req.body.closed1,
    }, {
      days: req.body.days2,
      opening: req.body.opening2,
      closing: req.body.closing2,
      closed: req.body.closed2,
    }]
  }, function(err, location) {
    if(err) {
      sendJsonResponse(res, 400, err);
    } else {
      sendJsonResponse(res, 201, location)
    }
  });
};

我得到TypeError:无法读取未定义的属性'split',即使我在Postman中选择了“X-www-form-urlencoded”,我的req.body在发帖时也是空的。在我的app.js文件中,我有:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
require('./app_api/models/db');

var index = require('./app_server/routes/index');
var indexApi = require('./app_api/routes/index');
var users = require('./app_server/routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'app_server', 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

我也尝试将app.use(bodyParser.urlencoded({ extended: false }));更改为app.use(bodyParser.urlencoded({ extended: true }));

任何帮助将不胜感激。我试图尽可能具体和详细。

-Edit-这就是我的模型:

var mongoose = require( 'mongoose' );

var openingTimeSchema = new mongoose.Schema({
  days: {type: String, required: true},
  opening: String,
  closing: String,
  closed: {type: Boolean, required: true}
});

var reviewSchema = new mongoose.Schema({
  author: String,
  rating: {type: Number, "default": 0, min: 0, max: 5},
  reviewText: String,
  createdOn: {type: Date, "default": Date.now}
});

var locationSchema = new mongoose.Schema({
  name: {type: String, required: true},
  address: String,
  rating: {type: Number, "default": 0, min: 0, max: 5},
  facilities: [String],
  coords: {type: [Number], index: '2dsphere', required: true},
  openingTimes: [openingTimeSchema],
  reviews: [reviewSchema]
});

mongoose.model('Location', locationSchema);

2 个答案:

答案 0 :(得分:0)

我实际上并没有看到您使用您在任何地方导入的路线。但是,无论您在何处使用app.use(myRoutes),任何插件/中间件都应移至上方

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

var index = require('./app_server/routes/index');
var indexApi = require('./app_api/routes/index');
var users = require('./app_server/routes/users');

答案 1 :(得分:0)

所以事实证明我的代码是正确的,我只是没有正确知道如何使用邮递员。我没有输入任何键值对!我觉得自己像个白痴。感谢您的帮助enter image description here