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) {
Huangism
  • 16,278
  • 7
  • 48
  • 74
  • What's in the line 64? – Umur Kontacı Aug 13 '14 at 01: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. http://plnkr.co/edit/MshJBB?p=preview Also try debug to see which method is said undefined. whether it is `$http` or the `success` – PSL Aug 13 '14 at 02:00
  • @UmurKontaci - The $http function is in line 64. – wellthatssomething Aug 13 '14 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. – wellthatssomething Aug 13 '14 at 12:28
  • 1
    Number of deps injected does not match number of arguments in constructor. $ state is $ http in ur case – PSL Aug 13 '14 at 12:51
  • @PSL YES! Not sure how I missed that... Thanks so much for the help. This was really stumping me. – wellthatssomething Aug 13 '14 at 13:14

2 Answers2

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!

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');
Kamrul
  • 7,175
  • 3
  • 31
  • 31
  • 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. – wellthatssomething Aug 13 '14 at 12:33