req.body is undefined mean app

戏子无情 提交于 2020-01-11 12:39:11

问题


I have an issue on my app. The simple way to tell you whats the problem let mme show you my code

var Meetup = require('./models/meetup');



module.exports.create = function (req, res) {
  var meetup = new Meetup(req.body);
  console.log(req.body);
  meetup.save(function (err, result) {
    console.log(result);
    res.json(result);
  });
}

module.exports.list = function (req, res) {
  Meetup.find({}, function (err, results) {
    res.json(results);
  });
}

console.log(req.body); outputs undefined console.log(result); outputs { __v: 0, _id: 5836ce6c38485021ec195a82 }while it should output { __v: 0,name:'text input' _id: 5836ce6c38485021ec195a82 }

here is my angular controller :

myApp.controller('meetupsController', ['$scope', '$resource', function ($scope, $resource) {
  var Meetup = $resource('/api/meetups');
$scope.meetups = []

  Meetup.query(function (results) {
    $scope.meetups = results;
  });


  $scope.createMeetup = function () {
    var meetup = new Meetup();
    meetup.name = $scope.meetupName;
    meetup.$save(function (result) {
      $scope.meetups.push(result);
      $scope.meetupName = '';
    });
  }
}]);

And my model :

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var Meetup = new Schema({
  name: String,
  text:String,

});


module.exports = mongoose.model('Meetup', Meetup);

Thank you for your help. PS i use bodyparser


回答1:


If req.body is undefined then you need to:

  • make sure you use the body-parser on the backend
  • make sure you pass the correct data on the frontend
  • make sure that the data passed by your frontend is in the correct place (body)
  • make sure that the data is in the correct format (JSON? URL-encoded?)
  • open the Network tab in the developer console of your browser and see what is transferred

See one of the answers to your own questions for more details:

  • schema error mean app


来源:https://stackoverflow.com/questions/40790647/req-body-is-undefined-mean-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!