AngularJS error: TypeError: v2.login is not a function

40,927

Solution 1

In my case, I was having an exact same issue as yours. However, coming across gkalpak's answer to such a scenario helped me out.

Turned out to be what I was calling was addBuddy() function, from a form named "addBuddy". The solution was to change the name of either of the two things to make one stand out or differentiable from the other. I changed the name of the form to "addBuddyForm" and voila! My function worked!

Here's a snippet of my case:

<form name="addBuddy" class="form-horizontal" novalidate>
...
<button class="btn btn-sm btn-info" ng-click="addBuddy()>Submit</button>

Which, I changed to:

<form name="addBuddyForm" class="form-horizontal" novalidate>
...
<button class="btn btn-sm btn-info" ng-click="addBuddy()>Submit</button>

...and it worked! :)

Solution 2

Edge case here, but I want to mention it for posterities' sake. I got this same error when using the controllerAs pattern with a form name with the same value as ng-submit. For example:

<form name="authCtrl.signUp" ng-submit="authCtrl.signUp()">

Throws: TypeError: v2.signUp is not a function

The solution was to change the name of the form to something different:

<form name="authCtrl.signUpForm" ng-submit="authCtrl.signUp()">

Solution 3

In AngularJS call the function from view it must be in the $scope.

JS

// exposes login function in scope
$scope.login = login;

HTML

<div class="container" ng-controller="AuthCtrl" style="max-width: 300px"> <!-- I notice here for include ng-controller to your main div -->
<form class="form-signin">       
  <h2 class="form-signin-heading" style="text-align: center">Please Sign In</h2>
  <input type="text" class="form-control" name="username" ng-model = "username" placeholder="Email Address" required="" autofocus="" />
    </br>
  <input type="password" class="form-control" name="password" ng-model = "password" placeholder="Password" required=""/>
    </br>
  <button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="login()">Login</button>   
</form>

Solution 4

This may not be specific to your problem, but I was also getting this error and it took a bit to figure out why.

I had named both a function and a variable the same, with the variable assigned in the function, and so the assignment of the variable was overriding the function and it was exploding on a second run.

You'll notice in the example the uploadFile() function as an upload.uploadFile = true; This was a wonderful file that was meant to be upload.uploadingFile - a flag used to control the behavior of a spinner. Once that was fixed, the issue went away.

Example:

(function()
{
  'use strict';

  angular.module('aumApp.file-upload')
  .controller('FileUploadCtrl', FileUploadCtrl);

  function FileUploadCtrl($scope, $http)
  {
    upload.uploadFile = function()
    {
      upload.uploadFile = true;
      var backendUrl = '/ua_aumcore/events/api/v1/events/uploadFile';
      var fd = new FormData();
      fd.append('file', upload.src);
      $http({ url: backendUrl, data: fd, method: 'POST', transformRequest : angular.identity, headers: { 'Content-Type' : undefined } })
      .then(function uploadSuccess(response)
      {
        upload.data = response.data;
        upload.message = "Uploaded Succesfully.";
        upload.uploadSuccess = true;
        upload.uploadingFile = false;
      },
      function uploadFailure(response)
      {
        upload.message = "Upload Failed.";
        upload.uploadSuccess = false;
        upload.uploadingFile = false;
      });
    };
  }
  FileUploadCtrl.$inject = ['$scope', '$http'];
})();

Solution 5

To be callable from the view, a function must be in the $scope. Add

$scope.login = login;

to the JS code of the controller.

You also need to actually use that controller. Change

<div class="container" style="max-width: 300px">

to

<div ng-controller="AuthCtrl" class="container" style="max-width: 300px">

This is all fundamental stuff. My advice would be to learn from an AngularJS tutorial before going further.

Share:
40,927
Nick
Author by

Nick

Updated on July 16, 2022

Comments

  • Nick
    Nick almost 2 years

    I would like to call the login function when I click the login button but keep getting the error message in the title. Can someone point out the error in my script?

    login.js code below:

    /*global Firebase, angular, console*/
    
    'use strict';
    // Create a new app with the AngularFire module
    var app = angular.module("runsheetApp");
    
    app.controller("AuthCtrl", function ($scope, $firebaseAuth) {
        var ref = new Firebase("https://xxxxx.firebaseio.com");
        function login() {
            ref.authWithPassword({
                email    : "xxxxx",
                password : "xxxx"
            }, function (error, authData) {
                if (error) {
                    console.log("Login Failed!", error);
                } else {
                    console.log("Authenticated successfully with payload:", authData);
                }
            });
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>

    And the code for login.html is also below:

    <div class="container" style="max-width: 300px">
        <form class="form-signin">       
          <h2 class="form-signin-heading" style="text-align: center">Please Sign In</h2>
          <input type="text" class="form-control" name="username" ng-model = "username" placeholder="Email Address" required="" autofocus="" />
            </br>
          <input type="password" class="form-control" name="password" ng-model = "password" placeholder="Password" required=""/>
            </br>
          <button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="login()">Login</button>   
        </form>
      </div>