【问题标题】:bodyparser error with mongoose mongodb nodejsmongoose mongodb nodejs的bodyparser错误
【发布时间】:2017-04-06 10:36:20
【问题描述】:

正在合并 2 个不同的应用程序,一个是身份验证应用程序,另一个是待办事项应用程序,用户输入数据并由应用程序显示。两者分开工作很好。当我合并它们时,第二个应用程序出现解析错误;我想我有问题。身份验证应用程序使用 app.use(bodyParser.json());第二个使用 app.use(bodyParser());我认为有冲突,但我不知道如何解决它,你能帮忙吗

这是我的 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) 显示未定义。 console.log(result) 显示 { __v: 0, _id: 583464c837cb810e045b1825 } 而它应该显示 { __v: 0,name:'text input' _id: 583464c837cb810e045b1825 }

这是我的模式模型:

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

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

});


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

【问题讨论】:

    标签: javascript angularjs node.js mongodb


    【解决方案1】:

    bodyParser() 构造函数是deprecated

    你应该改用这个:

    app.use(bodyParser.urlencoded());
    app.use(bodyParser.json());
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多