req.body在帖子上是空的

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了req.body在帖子上是空的相关的知识,希望对你有一定的参考价值。

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

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);
答案

我实际上并没有看到你使用你导入过的路线。但是任何插件/中间件都应该移动到使用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');
另一答案

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

以上是关于req.body在帖子上是空的的主要内容,如果未能解决你的问题,请参考以下文章

req.body 在帖子上为空

req.body在帖子上空了

nodejs中req.body为空的问题

Postman测试express中间件(body-parser),req.body为空的问题

删除时为空(req.body)(角度)

HttpContext.Current.User.Identity.Name 每次都是空的