字符串不是函数:错误AngularJS(工厂)MongoDB

时间:2015-03-01 21:22:19

标签: angularjs mongodb

我正在玩MEAN堆栈。我已经创建了一个从mongo db中删除的休息服务,但是当我尝试使用angular Factory方法并调用它时,我得到了上面的错误

 myApp.factory('methodFactory', ['$http', function ($http) {
         return {
            //id becomes undefined over here
            removeContact:function($http, id){ 
                //TODO add URL
                var url = '/contactlist/'+id;
                return $http({
                    method: 'DELETE',
                    url: url
                });
            }
        };
    }]);


 myApp.controller('AppControl', ['$scope','$http','methodFactory', function($scope,$http,methodFactory) {

    $scope.remove = function(id) {
            console.log(id); //able to print correct id

            methodFactory.removeContact(id).success(function(response){
                console.log("remv"+response);
                refresh();
            });//tthiss throws the error

            //this rest service works properly.
            /*$http.delete('/contactlist/'+id).success(function(response){
                console.log("remv"+response);
                refresh();
            });*/

        };
};

这节点服务器是什么样的

app.delete('/contactlist/:id',function(req,res) {
    var id = req.params.id;
    console.log(id);
    db.contactlist.remove({_id:mongojs.ObjectId(id)},function(err,doc){
        res.json(doc);
    });
    console.log("exiting delete")
});

我不确定工厂是否可以成为拨打休息服务的方式之一。什么可能导致问题? 错误

TypeError: string is not a function
    at Object.removeContact (http://localhost:3000/controllers/controller.js:10:20)
    at l.$scope.remove (http://localhost:3000/controllers/controller.js:85:23)
    at hb.functionCall (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js:198:426)
    at Cc.(anonymous function).compile.d.on.f (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js:215:74)
    at l.$get.l.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js:126:193)
    at l.$get.l.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js:126:419)
    at HTMLButtonElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js:215:126)
    at HTMLButtonElement.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular.min.js:32:363)

1 个答案:

答案 0 :(得分:0)

您的removeContact方法需要两个参数:

removeContact:function($http, id) ...

但你只用一个叫它:

methodFactory.removeContact(id) ...

我认为Id是一个字符串,但是它将被分配给函数$ http的第一个参数,根据您提供的代码,它必须是可执行的。