0

I have ionic/angular project.

I make a call to external api with:

  public getKeys() {

this.http.get('https://api.apify.com/v2/key-value-stores/myStorage/keys')
  .subscribe(
  data => this.dataKeys,
  err => this.handleError(err)
  );

}

My problem is that even thought i get valid response from server:

   {
  "data": {
    "items": [
      {
        "key": "1",
        "size": 52
      },
      {
        "key": "2",
        "size": 60
      }
    ],
    "count": 2,
    "limit": 1000,
    "exclusiveStartKey": null,
    "isTruncated": false,
    "nextExclusiveStartKey": null
  }
}

it doesnt get saved to this.dataKeys. Im calling this:

 ngOnInit() {
    this.createStorage();
    console.log(this.dataStorage);
    this.getKeys();
    console.log(this.dataKeys);
    this.players.push(this.player);
    console.log(this.players);
    this.postData(JSON.parse(JSON.stringify(this.player)), "3");
    this.getKeys();
    console.log(this.dataKeys);
  }

and all the logs from api remain undefined. Every help would be very appreciated.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • 1
    `console.log(this.dataKeys);` in ngOnInit is called before you get the response from your server so it will be undefined. As Hugo mentioned in his answer you are not assigning the returned data to `dataKeys`. `.subscribe(data => this.dataKeys = data)` – LLai Nov 30 '17 at 01:23
  • thanks a lot, this moved me one step forward. What Im getting now is `Response with status: 200 OK for URL: https://api.apify.com/v2/key-value-stores/myKey/keys` to console. but the response in network manager is` { "data": { "items": [ { "key": "1", "size": 52 }, { "key": "2", "size": 60 }, ], "count": 2, "limit": 1000, "exclusiveStartKey": null, "isTruncated": false, "nextExclusiveStartKey": null } }` how to parse the response to get to data structure? – user3564876 Nov 30 '17 at 12:29
  • Which console is printing out `Response with status: 200 OK for URL: https://api.apify.com/v2/key-value-stores/myKey/keys` `console.log(data)`? – LLai Nov 30 '17 at 14:19

1 Answers1

0

I believe you’re not assigning the returned data to your object.

You should do

public getKeys() {

this.http.get('https://api.apify.com/v2/key-value-stores/myStorage/keys')
  .subscribe(
  data => 
           {
                  this.dataKeys = data
            },
  err => this.handleError(err)
  );
Hugo Noro
  • 3,145
  • 2
  • 13
  • 19
  • thanks a lot, this moved me one step forward. What Im getting now is `Response with status: 200 OK for URL: https://api.apify.com/v2/key-value-stores/myKey/keys` to console. but the response in network manager is ` { "data": { "items": [ { "key": "1", "size": 52 }, { "key": "2", "size": 60 }, ], "count": 2, "limit": 1000, "exclusiveStartKey": null, "isTruncated": false, "nextExclusiveStartKey": null } } ` how to parse the response to get to data structure? – user3564876 Nov 30 '17 at 12:35
  • You have to do data.json(). I believe that will work. – Hugo Noro Nov 30 '17 at 12:41
  • Now console says ` [object Object]` – user3564876 Nov 30 '17 at 13:12
  • Not sure I’m following the last question. So from the network response it seems it’s already returning you a json object. – Hugo Noro Nov 30 '17 at 15:18