我不断收到TypeError:undefined不是一个函数

在MEAN堆栈应用程序中运行以下代码时,我不断收到上述错误:

$scope.completelesson = function(lessonindex, type) { //a variable that will be appended to 'level' in order to access the level property of the user var x = lessonindex + 1; var level = 'level' + x; var toupdate = { level: level, type: type, }; console.log(toupdate); $http({method: 'POST', url: '/users/updatelevel'}).success(function(response) { $location.path('/dashboard'); }); }; 

以下是完整的错误消息:

 TypeError: undefined is not a function at Scope.$scope.completelesson (http://localhost:3000/modules/dashboard/controllers/lesson.client.controller.js:64:13) at http://localhost:3000/lib/angular/angular.js:10795:21 at http://localhost:3000/lib/angular-touch/angular-touch.js:441:9 at Scope.$eval (http://localhost:3000/lib/angular/angular.js:12632:28) at Scope.$apply (http://localhost:3000/lib/angular/angular.js:12730:23) at HTMLButtonElement.<anonymous> (http://localhost:3000/lib/angular-touch/angular-touch.js:440:13) at http://localhost:3000/lib/angular/angular.js:2843:10 at forEach (http://localhost:3000/lib/angular/angular.js:325:18) at HTMLButtonElement.eventHandler (http://localhost:3000/lib/angular/angular.js:2842:5) 

奇怪的是,这个代码以前曾经工作过 – 突然停止了。 该函数一直运行直到$ http。 我知道这是因为console.log()logging正确的对象,但$ http请求从不logging到节点控制台。

我的AngularJS文件是最新的(1.2.22)。

任何想法可能导致这个错误消息,以及如何解决它?

谢谢您的帮助。

编辑:

这里是我的控制器定义的代码:

 angular.module('dashboard').controller('lessonController', ['$scope', 'Authentication', '$location', 'lesson', '$sce', '$http', function($scope, Authentication, $location, lesson, $sce, $state, $http) { 

@PSL解决了这个问题。

问题在于我的控制器定义。 我正在注入更多的依赖关系比在构造函数中的参数数量。 这就是为什么控制器将运行,直到$ http,因为我注入$状态,我应该注入$ http。

将我的控制器定义更改为以下代码修复了我的问题:

 angular.module('dashboard').controller('lessonController', ['$scope', 'Authentication', '$location', 'lesson', '$sce', '$http', function($scope, Authentication, $location, lesson, $sce, $http) { 

感谢所有的帮助!

尝试使用

 $http({method: 'POST', url: '/users/updatelevel', data: {}}).success(function(response) { $location.path('/dashboard'); 

要么

 $http.post('/users/updatelevel', {})success(function(response) { $location.path('/dashboard');