簡體   English   中英

req.body是未定義的平均應用程序

[英]req.body is undefined mean app

我的應用有問題。 告訴你這個問題的簡單方法讓我告訴你我的代碼

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) ; 輸出undefined console.log(result) ; 輸出{ __v: 0, _id: 5836ce6c38485021ec195a82 } ,輸出{ __v: 0,name:'text input' _id: 5836ce6c38485021ec195a82 }

這是我的角度控制器:

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 = '';
    });
  }
}]);

我的模特:

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

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

});


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

謝謝您的幫助。 PS我使用bodyparser

如果req.body未定義,那么您需要:

  • 確保在后端使用body-parser
  • 確保在前端傳遞正確的數據
  • 確保前端傳遞的數據位於正確的位置(正文)
  • 確保數據格式正確(JSON?URL編碼?)
  • 在瀏覽器的開發人員控制台中打開“網絡”選項卡,查看傳輸的內容

有關詳細信息,請參閱您自己問題的答案之一:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM