user3516604
user3516604

Reputation: 367

Get position in Array

i received a response from my API but i can't access the positions of array in this response.

fileTransfer.upload(this.imageURI, environment.restUrl + "Upload/addIonic", options)
      .then(data => {
        console.log('data');
        console.log(JSON.stringify(data));
        let response;
        console.log('bytesSent');
        console.log(data.bytesSent);
        response = data.response
        console.log('response');
        console.log(response);
        console.log('response[0]');
        console.log(response[0]);
});

console returns:

console.log: bytesSent
[19:51:03]  console.log: 1168539
[19:51:03]  console.log: response
[19:51:03]  console.log:
            [{"fd":"445bcc46-ad55-4079-95d0-9b0deaab7c4c","size":1168430,"type":"image/jpeg","filename":"ionicfile","status":"finished","field":"ionicfile","extra":{"Location":"https://easy-move.s3.amazonaws.com/445bcc46-ad55-4079-95d0-9b0deaab7c4c","Bucket":"easy-move","Key":"445bcc46-ad55-4079-95d0-9b0deaab7c4c","ETag":"\"69ede2190589f905ee8590446caf1cf7-1\"","size":1168430}}]
[19:51:03]  console.log: response[0]
[19:51:03]  console.log: [

Instead return a object, the array is returnin just "[" in position zero.

Upvotes: 1

Views: 56

Answers (1)

Barmar
Barmar

Reputation: 780974

response is apparently a JSON string, not an array, so response[0] returns the first character of the string. Use:

response = JSON.parse(data.response);

If you have control over the API, you should investigate why it's encoding this element, rather than leaving it as an array when it encodes the entire data. Double-encoding like this is rarely needed.

Upvotes: 7

Related Questions