角度放置法

时间:2015-12-17 07:41:17

标签: angularjs node.js mean-stack

我尝试使用$ http.put进行Angular更新。当我执行console.log时,数据成功从表单传递到客户端控制器,但是当我执行$ http.put请求时。它返回PUT http://localhost:3000/articles 500(内部服务器错误)。

这是客户端articles.controller.js:

$scope.updateArticle = function(){
    var data = {
        id:         $routeParams.id, 
        title:      $scope.article.title, 
        body:       $scope.article.body, 
        category:   $scope.article.category
    }
    console.log(data);

    $http.put('/articles', data).success(function(data, status){
        console.log('abc');

    });



    $location.path('/articles');


}

这是articles.js的服务器端路由:

router.put('/', function(req, res, next){
    var id = req.body.id;

    var data = {
        title: req.body.title, 
        category: req.body.category, 
        body: req.body.body
    };

    Article.updateArticle(id, data, function(err, article){
        if(err){
            console.log(err);
        }

        res.location('/articles');
        res.redirect('/articles');
    }); 
});

这是模型文章.js:

module.exports.updateArticle = function(id, data, callback){
    var title = data.title; 
    var body = data.body; 
    var category = data.category; 

    var query = {_id: id};

    Article.findById(id, function(err, article){
        if(!article){
            return next(new Error('Could not load article'));
        } else {
            article.title = title; 
            article.body = body; 
            article.category = category; 

            article.save(callback);
        }
    })
}

1 个答案:

答案 0 :(得分:0)

为什么不尝试直接定位“/ articles”的router.put路由?您的articles.js文件应如下所示:

router.put('/articles', function(req, res, next){
    console.log("Hit my route!");
    //etc... 
});