1
我在我的應用程序中遇到問題。簡單的方法告訴你什麼問題讓我告訴你我的代碼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)
;輸出未定義 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
你確定型的請求是POST?和bodyParser包含在所有路線之前? –
非常感謝你,我沒有包括所有路線之前的bodyparser。 10q這麼多! –
Np。樂意效勞。 –