6

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) {
6
  • What's in the line 64? Commented Aug 13, 2014 at 1:50
  • By the way did you have the dependency injected for $http. Something like app.controller('controller',['$scope','blah', function($scope, blah,$http){//and trying to access $http ?}]) can you show your controller definition? That could also cause an error like this. plnkr.co/edit/MshJBB?p=preview Also try debug to see which method is said undefined. whether it is $http or the success Commented Aug 13, 2014 at 2:00
  • @UmurKontaci - The $http function is in line 64. Commented Aug 13, 2014 at 12:27
  • @PSL - Yes I have the dependency injected for $http. I'll add the code for my controller definition into my original post. Commented Aug 13, 2014 at 12:28
  • 1
    Number of deps injected does not match number of arguments in constructor. $ state is $ http in ur case Commented Aug 13, 2014 at 12:51

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Can't believe I forgot to send data in! Regardless - I tried both adding my object 'toupdate' and adding an empty object, yet I still receive the same error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.