1

I'm trying to call a function from my dataservice factory but it throws an error Cannot read property 'login' of undefined

Here is my dataservice factory

(function(){
  'use strict';
  // dataservice factory
 angular
.module('app')
.factory('dataservice', dataservice);

 dataservice.$inject = ['$http'];

 function dataservice($http) {

  return {
    login  : login
  }


function login(username, password, callback) {
    $http.post('/data', { username: username, password: password })
        .success(function (response) {
          console.log(response);
       });
    }  
 }
})();

Here is my controller

(function(){

'use strict';

angular
  .module('app')
  .controller('TestController', TestController);

TestController.$inject = ['dataservice'];

function TestController(dataservice) { 

  var vm = this;

  vm.login = login;



function login() {
console.log('is this called?');
    vm.loading = true;
    dataservice.login(vm.username, vm.password, function (result) {
        console.log(result);
    });
 }

 }
})();

login function it is called when i press login from view , since i get: is this called? in my console.

2
  • missing a } after function data service Commented Sep 12, 2016 at 12:26
  • I checked syntax and it is ok. Commented Sep 12, 2016 at 12:37

2 Answers 2

3

You need to right the callback in a then block

function login() {
    console.log('is this called?');
    vm.loading = true;
    dataservice.login(vm.username, vm.password).then(function (result) {
        console.log(result);
    }, function(error){
    });
 }

This is the correct syntax.

Also you can use this service syntax

   function login(){
        var d = $q.defer();
        $http({
            method: 'POST',
            url: '/data',
            data:{
                username : username,
                password : password
            }
        }).success(function(response){
            d.resolve(response);
        }).error(function(response){
            d.reject(response);
        });
        return d.promise;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

I tried using this code but i got the same error Cannot read property 'login' of undefined , I copied your service and controller function . Unfortunately it doesn't work
can you create a fiddle or plunkr, because i don't see any reason to get a error like undefined method..
you are getting your service as undefined, see if it is added in index.html and defined properly?
I minify my code in a single file , and from index.html file is called login function that points to login() in controller. Until here , it is okay since i it prints my console.log line . When it should call dataservice function it throws an error. I have also tried using a simple function from dataservice , but I still get the same error Cannot read property '(function name)' of undefined
don't use $q when $http already return a promise, just return the $http.
2

I corrected your example to more common and readable syntax. Check this fiddle to see it in action.

(function () {
    'use strict';
    // dataservice factory
    var app = angular.module('app', []);
    app.factory('dataservice', ['$http', function ($http) {

        function login(username, password, callback) {
            console.log('Login in dataservice...');
            $http.post('/data', {username: username, password: password})
                .then(function (response) {
                    console.log(response);
                }, function (error) {
                    console.warn(error);
                });
        }

        return {
            login: login
        };

    }]);

    app.controller('TestController', ['dataservice', function (dataservice) {
        var vm = this;
        vm.login = login;
        function login() {
            console.log('is this called?');
            vm.loading = true;
            dataservice.login(vm.username, vm.password, function (result) {
                console.log(result);
            });
        }
    }]);
})
();
  1. You should pass second argument to app.module as an empty array (if you have no dependencies on other modules)
  2. Use $http.then because .success is depricated.
  3. Pass second argument to app.factory or app.controller as an array with a list of dependencies and the function at last. In your example, you didn't inject any dependencies because TestController.$inject = ['dataservice']; was running too late.

1 Comment

I do not know what was my problem , but It worked now without changes. Anyway thank you for your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.