在MEAN app中获取404 Not PUT请求

时间:2016-05-10 09:34:31

标签: angularjs node.js mongodb express mean-stack

我的事件详细信息页面上有一个“编辑”按钮,该按钮将转到新页面,用户可以在该页面上更新当前所选事件。我使用GET获取单个/所有事件和POST来创建新事件没有任何问题。但我仍然坚持更新现有事件并不断收到404错误:PUT http://localhost:3000/api/events 404(未找到) 在我的服务器路线上,我有:



//return event-details
app.get('/api/events/:id', events.getEventById);

//update event
app.put('/api/events/:id', events.updateCurrentEvent);




服务器端事件控制器:



exports.updateCurrentEvent = function(req, res) {

     Event.findById(req.params.id, req.body, function(err, event) {
        var event = req.body;
        if(!event) {
            res.statusCode = 404;
            res.send({ error: 'Not found'});
        }
        event.title = req.body.title;
        event.desc = req.body.desc;
        event.date = req.body.date;
        event.duration = req.body.duration;
        event.address = req.body.address;
        event.city = req.body.city;
        event.state = req.body.state;

        event.save(function (err) {
            if (!err) {
                log.info("event updated");
                res.send({ status: 'OK', event:event });
            } else {
                if(err.name == 'ValidationError') {
                    res.statusCode = 400;
                    res.send({ error: 'Validation error' });
                } else {
                    res.statusCode = 500;
                    res.send({ error: 'Server error' });
                }
                log.error('Internal error(%d): %s',res.statusCode,err.message);
            }
        });

    });




我的$资源服务:



app.factory('mvEvent', function($resource) {
    var EventResource = $resource('/api/events/:_id', {_id: "@id"}, {
        update: {method:'PUT', isArray:false}
    });

    return EventResource;
});




我的客户端控制器:



angular.module('app').controller('mvUpdateEventCtrl', function($scope, $routeParams, $location, mvEvent) {

    $scope.event = mvEvent.get({_id:$routeParams.id})
        .$promise
            .then(function(event) {
                $scope.event = event;
                console.log($scope.event);
                $scope.title =$scope.event.title;
                $scope.desc = $scope.event.desc;
                $scope.date = $scope.event.date;
                $scope.duration = $scope.event.duration;
                $scope.address = $scope.event.address;
                $scope.city = $scope.event.city;
                $scope.state = $scope.event.state;
            });

    $scope.updateEvent = function() {
        $scope.event.$update(function() {
        }, function(error) {
            $scope.error = error.data.message;
        });
    }
});



 我的客户端路线:



var app = angular.module('app', ['ngResource', 'ngRoute', 'ui.bootstrap']);

app.config(function($routeProvider, $locationProvider){

    $locationProvider.html5Mode(true);

    $routeProvider
        //events route
        .when('/events', {
            templateUrl: '/partials/events/event-list',
            controller: 'mvEventListCtrl'
        })
        //events detail route
        .when('/events/:id', {
            templateUrl: '/partials/events/event-details',
            controller: 'mvEventDetailsCtrl'
        })
        //update event route
        .when('/events/:id/update', {
            templateUrl: '/partials/admin/event-update',
            controller: 'mvUpdateEventCtrl'
        })
});




在每个文本字段中显示事件详细信息是我能得到的。只要我点击“更新活动”'按钮我得到404错误,它似乎位于我的服务器端代码中的某处。我已经看到了使用findById然后使用save或findByIdAndUpdate实现PUT请求的相当多的不同方法,有或没有routeParams。我想知道是否有标准的方法来做到这一点。在此先感谢!!

1 个答案:

答案 0 :(得分:0)

从服务器端控制器中删除行var event = req.body;。首先,它不是必需的。其次,它与Event.findById回调返回的文档的名称相同,并且被变量声明覆盖。

 exports.updateCurrentEvent = function(req, res) {

     Event.findById(req.params.id, req.body, function(err, event) {

        var event = req.body;  // <<==== Remove this line

        if(!event) {
            res.statusCode = 404;
            res.send({ error: 'Not found'});
        }

        event.title = req.body.title;
        event.desc = req.body.desc;
        event.date = req.body.date;
        event.duration = req.body.duration;
        event.address = req.body.address;
        event.city = req.body.city;
        event.state = req.body.state;

        event.save(function (err) {
            if (!err) {
                log.info("event updated");
                res.send({ status: 'OK', event:event });
            } else {
                if(err.name == 'ValidationError') {
                    res.statusCode = 400;
                    res.send({ error: 'Validation error' });
                } else {
                    res.statusCode = 500;
                    res.send({ error: 'Server error' });
                }
                log.error('Internal error(%d): %s',res.statusCode,err.message);
            }
        });
    });
}