0

I downloading JSON data from an address by code:

  return {
        all: function () {

         // var dfd = $q.defer();
           var promise =  $http.get("http://127.0.0.1:5000/mobile/").then(function (response) {
             console.log(response.data.hits);

             return response.data;
           });
          // Return the promise to the controller


          return promise;

        }
}

Console log returns array of Objects:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

Any open of the object gives:

0: Object
   $$hashKey: "object:20"
   _id: "AVCZPOdoX5ZoBStgqx-J"
   _index: "fb_events"
   _score: 1
   _source: Object
   _type: "event"
   __proto__: Object

As with the console log level I can get to the field _id?

Edit:

He wants to put the data into an array so as to have access to them through another loop which will return me an interesting record. Like:

get: function(chatId) {
          for (var i = 0; i < chats.length; i++) {
            if (chats[i].id === parseInt(chatId)) {
              return chats[i];
            }
          }
          return null;
        }
10
  • What's your question again? Can you re-write? Commented Oct 24, 2015 at 12:39
  • var result = [Object, Object, Object] var id = result[0]._id; Commented Oct 24, 2015 at 12:40
  • I want to show _id field like response.data.hits._id, but is does not work Commented Oct 24, 2015 at 12:43
  • As your log shows, response.data.hits is a array containing 19 objects. That array doesn't have any _id field. Each of the 19 objects in the array probably has one: response.data.hits[0]._id, esponse.data.hits[1]._id, etc. Commented Oct 24, 2015 at 12:45
  • 1
    If you need to parse all the data then yes... Ill add a proper answer Commented Oct 24, 2015 at 12:49

1 Answer 1

1

Here you go following comments...

EDITED

var chats = []; // global in this instance

var promise =  $http.get("http://127.0.0.1:5000/mobile/")
.then(function (response) {
   console.log(response.data.hits);
   for(var i=0; i < response.data.hits.length; i++)
   {
    console.log(response.data.hits[i]._id);
   }

   chats = response.data.hits; // assign hits[] to chats

   return response.data;
 });

if(chats.length !== 0)
  // do something
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, it works. How to assign the response.data to a global array so that it is available for other functions?
That would depend on your current architecture mate? You could just define a global var and when the promise returns, assign the data to the global variable? You would have to check it has been assigned first though, will update my answer

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.