wellthatssomething
wellthatssomething

Reputation: 155

I keep getting TypeError: undefined is not a function

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

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

Here's the full error message:

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) 

The weird thing is, this code used to work before - all of sudden it stopped. The function runs all the way until $http. I know this because the console.log() logs the correct object, but the $http request is never logged to the node console.

My AngularJS files are up to date (1.2.22).

Any idea what could be causing this error message and how to fix it?

Thanks for the help.

EDIT:

Here is the code code for my controller definition:

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

Upvotes: 6

Views: 2479

Answers (2)

wellthatssomething
wellthatssomething

Reputation: 155

@PSL solved this one.

The problem lied in my controller definition. I was injecting more dependencies than the number of arguments in the constructor. That's why the controller would function until $http, because I injected $state where I should be injecting $http.

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

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

Thanks for the help all!

Upvotes: 1

Kamrul
Kamrul

Reputation: 7301

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

Upvotes: 0

Related Questions