简体   繁体   中英

What is wrong with my POST, resulting in an empty req.body

I am building a simple MySQL, Express, Angular, Node app and using Sequelize as my ORM. When I create a new survey, the values I am sending do not get set, as the req.body is an empty object.

server.js

'use strict';

var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var bodyParser = require('body-parser');
var models = require('./server/models/');
var routes = require('./server/routes');

app.use(bodyParser.urlencoded({ 
  extended: true 
}));
app.use(express.static(process.cwd() + '/public'));

//App routes
app.use(routes(express.Router()));

app.get('/*', function (req, res) {
  res.sendFile('index.html', {
    root: './public'
  });
});

app.listen(port, function() {
    console.log('Server running on port %s ', port);
});

app.js

'use strict';

// Declare app level module which depends on views, and components
var app = angular.module('myApp', []);

    app.controller('SurveyController', ['$scope', '$http', function($scope, $http) {

    //Get all Surveys
    $http.get("/surveys")
    .then(function(response) {
        $scope.surveys = response.data;
    });

    $scope.create = function() {
      $http.post("/surveys",{
        question: $scope.question, 
        answers: $scope.answers,
        user: $scope.user
      })
      .then(function(response) {

      });
    }
}]);

model

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Survey = sequelize.define('Survey', {
    question: DataTypes.STRING,
    answers: DataTypes.STRING,
    user: DataTypes.STRING
  }, {
     underscored: true,
     classMethods: {
       associate: function(models) {
         // associations can be defined here
       }
     }
  });
return Survey;
};

routes.js

var surveys = require('./controllers/surveys');

module.exports = function (router) {
  //routes
  router.get('/surveys', surveys.get);
  router.get('/surveys/:id', surveys.getOne);
  router.post('/surveys', surveys.create);
  router.put('/surveys', surveys.update);
  router.delete('/surveys', surveys.delete);

  return router
};

controller

//Create a new survey
create: function(req, res) {
    //this req.body and req.params are both empty
    Survey.create(req.body)
    .then( function(newSurvey) {
        res.status(200).json(newSurvey);
    })
    .catch( function(error) {
        res.status(500).json(error);
    })
},

I recently had a similar problem here: Why is my req.body always empty on POST? but the solution for that did not work for this issue. I am presuming it is similar but the solution has alluded me so far.

Don't need to stringify

$http
  .post("/surveys", {
     question: $scope.question, 
     answers: $scope.answers,
     user: $scope.user
   },
   {
     headers: { 'Content-Type': 'application/json; charset=UTF-8'}
   })

Per many comments above (many thanks to all), I solved this by changing my bodyParser implementation from

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

to

app.use( bodyParser.json() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM