1

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 Answers1

0

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:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177