3
.controller('LoginConnect', ['$scope', 'connecting',
function($scope, connecting){
    $scope.user = {};
    $scope.connect = function($scope, $q){
      connecting();
    };
  }
])
.factory("connecting", [ function($scope,$q, $http){
      var deferred = $q.defer();
      $http({
          method: 'POST',
          url: "http://api.tiime-ae.fr/0.1/request/login.php",
          headers: {'Content-Type': 'application/x-www-form-urlencoded'},
          transformRequest: function(obj) {
              var str = [];
              for(var p in obj)
              str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
              return str.join("&");
          },
          data: {login: $scope.user.login, password: $scope.user.password}
          })
      .success(function(result){
         deferred.resolve(result);
         var promise = deferred.promise;
         promise.then(function(result){
           jsonTab = angular.fromJson(result);
            $scope.result = result["data"];
        $scope.user.token = result["data"];
          });
        })
      }
    ])

Hi, I'm new in Angular JS and I have the following error: TypeError: Cannot read property 'defer' of undefined.

How can we fix this?

2 Answers 2

1

You wrote the wrong syntax for your connecting factory. Change it to:

.factory("connecting", ["$scope", "$q", "$http", function($scope,$q, $http){

You are using the inline array annotation syntax for injecting Angular dependencies, so you need to name the exact name in the lists like above: "$scope", "$q", "$http". Since you have not defined any name there, the variable $q was getting null.

Read more on this: https://stackoverflow.com/a/35336414/2405040

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

Comments

0

Btw: please don't use the named argument syntax. It's harder to read, more to write and so nice at all.

Do something like:

 angular.module('app')
        .factory('MyFactory', MyFactory);

    /*@ngInject*/
    function MyFactory($scope, $q) {
.....

}

Comments

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.