Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {
like image 222
wellthatssomething Avatar asked Aug 13 '14 01:08

wellthatssomething


People also ask

What is the reason for getting TypeError undefined is not an object?

The “TypeError: 'undefined' is not an object” error occurs when a property is accessed or a method is called on an undefined object. This error is shown only on safari browser.

Why is JavaScript saying my function is not defined?

You're Calling the Function Before It's Defined If the code that calls your JavaScript function precedes that function's definition in your HTML document, you will come across the function is not defined error.

What causes a TypeError?

A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or. when attempting to modify a value that cannot be changed; or. when attempting to use a value in an inappropriate way.


1 Answers

@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!

like image 75
wellthatssomething Avatar answered Nov 01 '22 17:11

wellthatssomething