我正在尝试实现一个简单的编辑功能,但它不起作用。我删除并开始工作。我在put请求中一直收到500错误。我尝试过findIdAndUpdate,我也尝试过FindOne。我得到的错误是它无法加载资源。但是,如果我做一个获取请求它工作正常。如果这有所不同,get请求也会返回304.
如果我以curl发送请求,我会TypeError: Cannot read property 'id'
控制器和服务
app.factory('gameService', function($resource){
return $resource('/api/games/:id', {id:'@id'},
{'update': {method:'PUT'}}
);
});
app.controller("gameController", function($scope, $http, gameService){
$scope.games = [];
$scope.newGame = {name: '', platform: ''};
$scope.editMode = false;
$scope.games = gameService.query();
$scope.edit = function(game){
$scope.editMode = true;
$scope.newGame = gameService.get({id: game._id});
};
$scope.update = function(){
gameService.update({id: $scope.newGame._id}, function(response){
$scope.games = gameService.query();
$scope.newGame = {name: '', platform:''};
});
$scope.editMode = false;
};
});
API /快递/猫鼬
router.put('/games/:id', function(req, res, next) {
Game.findById(req.parms.id, function (err, game) {
if (err) {
return res.send(err);
}
game.name = req.body.name;
game.platform = req.body.platform;
game.save(function(err){
if (err) {
return res.send(err);
}
res.json({message:'Game Updated'});
});
});
});
HTML
<input required type="text" placeholder="Game Name" ng-model="newGame.name" /> <br/><br/>
<input required type="text" placeholder="Platform" ng-model="newGame.platform" /> <br/><br/>
<input class="button" type="submit" value="Post" ng-click="post()" ng-hide="editMode" />
<input class="button" type="submit" value="Update" ng-click="update()" ng-show="editMode"/>
模型
var mongoose = require ('mongoose');
var GameSchema = new mongoose.Schema({
name: String,
platform: String
});
mongoose.model ('Game', GameSchema);
答案 0 :(得分:0)
req.parms.id
应为req.params.id
。