Angular Js : '$q.defer is not a function' error
Asked Answered
H

2

8

After Refering this Link , I am trying to get JSON data into my angular service.

Service:

.factory('restservice', ['$rootScope','$http', '$q', '$log',
function($rootScope,$q, $http) {
return {
    getData: function() {
        var defer = $q.defer();
        $http.get('xyz.com/abc.php', { cache: 'true'})
        .success(function(data) {
            defer.resolve(data);
        });

        return defer.promise;
      }
};
}])


Controller:

.controller('RestaurantsCtrl', function ($scope,$http, restservice,restViewservice){

      restservice.getData().then(function(data) {
      $scope.Restaurants = data;
    });

})


After Implementing this console says '$q.defer is not a function'.

What's the issue here? Please Help ...!! Am new to Angular Js so forgive if something is wrong.

Handclasp answered 3/10, 2015 at 14:25 Comment(3)
You shouldn't use $q.defer, just return your $http.get(..). also, success is deprecated - use then instead.Crat
thanks for that ! :)Handclasp
Such a nice thing that people ask their questions on Stackoverflow, I'm sure this question and also the answer solved many people's problems. Thanks for thisHalda
T
25

You have wrong service definition:

factory('restservice', ['$rootScope','$http', '$q', '$log',
function($rootScope, $q, $http) {

Should be:

factory('restservice', ['$rootScope', '$http', '$q', '$log',
function($rootScope, $http, $q, $log) {

Common mistake :)

Tedious answered 3/10, 2015 at 14:27 Comment(7)
Thanks a lot ! That was quick ! worked ! i feel stupid for being a noob in angular now. i thought that won't matter. well thanksHandclasp
Everyone makes this type of errors, me also, after 2 years of angular. No worries :)Tedious
@Maxxim Shoustin can you help with cache. the above http get object is not getting cached. I am using anular js to build app with ionic. this default caching wont work in mobile apps ?Handclasp
@SumeetDarade on mobiles like Cordova all data should be stored in native (a.e. SharePreferences for Android or UserDefaults for iOS or sqlite if you have a lot of data and need some manipulation). Also since angular is MVC, the model should be defined in native also and not in services like in Web.Tedious
@SumeetDarade Further, All http calls I suggest you to make in native and not in Angular (aka JS) because if you expect a lot of users, some devices have bugs with sending http dataTedious
actualy its the matter of one JSON object thats used to populate list. since its just a single object thats of importance i planned on caching it. Cordova has plugins for storage options like sql. But , i didnt wanted to do that just for single json object and was finding a easier way out. about http , ionic uses angular js so i had toHandclasp
To clarify this answer further, the inject parameters and function parameters differ. In the answer above, the $log was missing from the function parameters.Fisken
B
0

We faced the same error and resolved now:

Please refer angular 1.6.1 version file, since older version of angular gives the above error.

Bouldin answered 8/2, 2017 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.