简体   繁体   中英

bodyparser error with mongoose mongodb nodejs

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

here is my api

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) displays undefined. console.log(result) displays { __v: 0, _id: 583464c837cb810e045b1825 } while it should display { __v: 0,name:'text input' _id: 583464c837cb810e045b1825 }

here is my schema model :

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

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

});


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

The bodyParser() constructor is deprecated .

You should use this instead:

app.use(bodyParser.urlencoded());
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