0

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!!

5
  • Your factory code is invalid, Factoy should return only public methods, you are creating them inside a forEach loop. It is correct code, or invalid copy-paste? Commented Apr 27, 2015 at 10:16
  • this is the correct code..when i console inside the loop i get the value but could not return it. Commented Apr 27, 2015 at 10:19
  • So in a nutshell you want to call more thna one AJAX calls, for each deviceId and agregate all results into one list? How many elements deviceIds has? Commented Apr 27, 2015 at 10:26
  • 2 value max which is a dynamic value. yes i need to call for both the values. Commented Apr 27, 2015 at 10:31
  • I've figured that out:) I've created a sample code, that could help you with your problem, with some description. Commented Apr 27, 2015 at 10:40

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

5 Comments

It shows you the concept, it may contain few syntax bugs, due to no IDE near me:) I would advise to do it step by stemp, do not implement whole code at start, because it is complicated:)
am getting error TypeError: angular.map is not a function on searching regarding google is suggesting angular google map
@user2590035 my bad:) map is a javascript function :) will add some refs
yeah plz.. I am out of idea's with this.
I've added few points, you should do it step by step, then you could work it out:)

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.