2016-08-21 66 views
1

我正在尝试实现一个简单的编辑功能,它不起作用。我删除并获得工作。我在收到请求时一直收到500错误。我试过findIdAndUpdate,我也试过FindOne。我得到的错误是它无法加载资源。但是,如果我做了一个获取请求,它工作正常。如果有差别,GET请求也返回一个304Mongoose,Angular,Express PUT将不起作用

如果我卷曲发送请求我得到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

req.parms.id应该在你的猫鼬/快递API路线req.params.id

+0

修正了这个问题。现在我有一个新问题。 game.save正在清除名称和platofmr字段。所以它会出现req.body.name不正确 – CodyK

+1

我明白了,谢谢。 – CodyK