0

I have a schema problem. I dont get the right schema in my api. here is my api :

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

    module.exports.create = function (req, res) {
      var meetup = new Meetup(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);
      });
    }

The console.log displays { __v: 0, _id: 58343483ff23ad0c40895a00 } while it should display { __v: 0, name: 'Text input', _id: 58343076b80874142848f26e }

here is my model:

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

var Meetup = new Schema({
  name: String,

});


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

1 Answers1

0

If req.body is undefined (as you wrote in the comments) then obviously new Meetup(req.body); cannot populate the new objects with any data (like {name: 'Text input'} or anything else) since it is called with undefined as an argument.

Make sure you use the body-parser and that you pass the correct data in your request.

Also, check for errors. Every callback that takes the err argument should be in the form of:

module.exports.list = function (req, res) {
  Meetup.find({}, function (err, results) {
    if (err) {
      // handle error
    } else {
      // handle success
    }
  });
}

How to track the problem:

  • 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?)
  • add console.log(req.body) after new Meetup(req.body); to know what you save
  • open the Network tab in the developer console of your browser and see what is transferred
rsp
  • 107,747
  • 29
  • 201
  • 177
  • 10q for your reply but can you be more specific. I am new can u tell me where i should look. is it in my angular controller or on the server-side – Mikyas TADESSE Nov 22 '16 at 15:38
  • @MikyasTADESSE see my updated answer. You need to parse body on the server side but you also make sure that you send the correct data in the body of your requests from your angular app. use the developer tools to see what is actually transferred. – rsp Nov 22 '16 at 15:43
  • 10q you so much. Am merging 2 different applications one is an authentification app and the other is a todo app where user input data and the app displays it. The two work fine seperately. when i merged them there is a parse error on the second app; I think i got the issue the authentification app uses app.use(bodyParser.json()); and the second one uses app.use(bodyParser()); I think there is a conflict but i dont know how to solve it can you help – Mikyas TADESSE Nov 22 '16 at 15:48