Ashwin
Ashwin

Reputation: 35

Error: [$injector:undef] is occurring in service angularjs

Am getting an error Error: [$injector:undef] when am using service and http. I couldn't find why is it coming after changing all the changes that has been suggested for this particular error. Kindly check the below code

mainApp.controller('deviceDataController',["$scope","mainService", function ($scope,mainService) {
                console.log(mainService);
                mainService.deviceDataVar().success(function (data) {
                   // $scope.deviceDetails = mainService.devices;
                   console.log(data);
                });
            }]);
            
                
            mainApp.factory("mainService",["$http", function ($http) {

                angular.forEach(deviceIds, function(value, key) {
                    var timezone = user_response.timezone;
                    var DataUrl = LAX_API + "device_info.php?deviceid=" + value + "&limit=288&timezone=" + timezone;

                    return {
                        deviceDataVar: function () {
                           return $http.get(DataUrl).success(function (response) {
                                devices = response.data;
                                return devices;
                            }).error(function (data, status, headers, config) {
                                // log error
                                console.log(data)
                            });;
                        }
                    }

                });
            }]);

kindly help me out with my issue

Thanks!!

Upvotes: 0

Views: 908

Answers (1)

Beri
Beri

Reputation: 11620

Your factory declaration is not valid.

  1. Your factory should return only single method
  2. Create a method that returns $http promises
  3. Agregate all promises inside $q, that will wait for all of them to return a response
  4. Agregate all responses
  5. Return them inside a promise- you cannot return a value, because AJAX calls are async.

Your factory should look like this:

      mainApp.factory("mainService", function ($http, $q) {
                /* generate single link for deviceID */
                function getDevice(value){
                   var timezone = user_response.timezone;
                   var dataUrl= LAX_API + "device_info.php?deviceid=" + value + "&limit=288&timezone=" + timezone; 
                   return $http.get(dataUrl);
                 }

                function deviceDataVar(){
                   // map method will transform list of deviceIds into a list of $http promises
                   var allRequests = deviceIds.map(getDevice);
                   // this promise will wait for all calls to end, then return a list of their results
                   $q.all(allRequests).then(function(arrayOfResults) { 
                   // here you will have all responses, you just need to agregate them into single collection.
                   // secondly you will need to wrap them into a promise and return
                   return ...
                 });

               }

                  /* Return only public methods, at the end */
                return {
                    deviceDataVar: deviceDataVar
                }
        });

(I was writing this on fly, so there could be few mistakes :), but the conception is right though )

Useful links:

  1. Aggregating promises: angular -- accessing data of multiple http calls - how to resolve the promises
  2. Array.prototype.map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
  3. How to apss a promise to a factory: How can I pass back a promise from a service in AngularJS?

UPDATE:

To make it work by invokiing small steps you should :

  1. Mock the factory method, let it return a hardcoded value. Return it through a promise.
  2. replace hardcoded value with single (hardcoded) URL to a selected device.
  3. use aggregation ($q.all) for this single $http call.
  4. replace single $http with array made from deviceIds list.

Upvotes: 1

Related Questions