I keep getting TypeError: undefined is not a function

I keep getting the above error when running the following code in the MEAN stack application:

$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'); }); }; 

The complete error message is displayed here:

 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) 

It is strange that this code was used before - suddenly it stopped. Function works up to $ http. I know this because console.log () registers the correct object, but the $ http request is never logged in the node console.

My AngularJS files have been updated (1.2.22).

Any idea what might cause this error message and how to fix it?

Thanks for the help.

EDIT:

Here is the code to define my controller:

 angular.module('dashboard').controller('lessonController', ['$scope', 'Authentication', '$location', 'lesson', '$sce', '$http', function($scope, Authentication, $location, lesson, $sce, $state, $http) { 
+6
source share
2 answers

@PSL solved this one.

The problem is with my controller definition. I introduced more dependencies than the number of arguments in the constructor. This is why the controller will work until $ http, because I entered $ state, where I need to enter $ http.

Changing my controller definition to the following code fixed my problem:

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

Thanks for helping everyone!

+1
source

try using

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

or

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

Source: https://habr.com/ru/post/973740/


All Articles