我尝试使用angularjs http.post方法将表单数据发布到mongodb(moongose),我收到404错误,不确定我是否正确执行此操作。它抱怨它找不到网址 - > POST http://localhost:5000/tournaments/56bf2ea58e5ea932088ff356/games 404(未找到)
######这是我的api路由器router.post('/tournaments/:id/games', function(req, res) {
var newGame = {};
newGame.id = req.body.id;
newGame.gameDate = req.body.gameDate;
newGame.homeTeam = req.body.homeTeam;
newGame.awayTeam = req.body.awayTeam;
newGame.homeTeamScore = req.body.homeTeamScore;
newGame.awayTeamScore = req.body.awayTeamScore;
newGame.hadOT = req.body.hadOT;
newGame.hadSO = req.body.hadSO;
Tournament.update({ "_id": req.params.id}, {$push: {games: newGame}}, function(err, model) {
if(err) {
res.send(err)
} else {
res.json(model)
}
});
});
这是在控制器中
vm.isEditing = false;
vm.newGame = {
awayTeam: '',
homeTeam: '',
homeTeamScore: 0,
awayTeamScore: 0
};
vm.showModal = function() {
vm.isEditing = true;
}
vm.hideModal = function() {
vm.isEditing = false;
vm.addGameForm.$setPristine();
vm.isSaving = true;
}
vm.addGame = function() {
vm.isSaving = true;
vm.newGame.id = vm.selectedTournament.games.length + 1;
$http({
method : 'POST',
url : '/tournaments/'+tournamentId+'/games',
data : $.param($scope.vm.newGame),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response){
$scope.vm.newGame = {};
$scope.games = response;
console.log(response);
})
.error(function(data) {
console.log('Error: ' + data);
});
vm.addGameForm.$setPristine();
vm.hideModal();
}