0

I am having some trouble getting the actual data out of the return from the api call. I can see the values output into the log, but when I try to access it using data[0].admin_url it returns undefined.

this.apiService
    .getCCUrls()
    .then(function(data){
        console.log(data); //output in the image below
        console.log(data[0].admin_url); //returns undefined
    });

Which calls this:

APIService.prototype.getCCUrls = function() {
    return this.makePublicApiRequest({
        method: 'GET',
        url: '/v1/cc-instances',
    });
};

There is some simple utility code in between, but eventually it gets to this function...

APIService.prototype.makeRequest = function(config) {
    return this.$http(config)
               .then(function(response) {
                   // If we get a replacement token, update it.
                   this.authService.checkResponseHeaders(response);

                   var data = response.data;
                   if (data.data) {
                       return data.data;
                   }
                   if (data) {
                       return data;
                   }
                   return response;
               }.bind(this));
};

Lastly, this is what is output in the console.log:

enter image description here

So, in my first set of code, how do I get to the admin_url, agent_url, or type variables?

mydoglixu
  • 934
  • 1
  • 7
  • 25
  • 3
    Where is the `data[0].admin_url` call? – Andreas Jun 02 '18 at 11:21
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – georgeawg Jun 02 '18 at 12:58
  • @andreas I tried doing console.log(data[0].admin_url); but it just returned undefined. Not sure why. – mydoglixu Jun 02 '18 at 13:30
  • Repeating the same things doesn't make them better. Where did you log `data[0].admin_url`? In the last `.then()` (if so, then the image can't be true) or somewhere else? – Andreas Jun 02 '18 at 15:03
  • @andreas I just edited the post to make it clearer. The image itself is from the very first log. The second console.log is what returns undefined. – mydoglixu Jun 02 '18 at 16:22
  • 1
    And what about `console.log(data[0]);`? – Furgas Jun 02 '18 at 17:06

2 Answers2

0

This problem could be caused by the underscore being a \u0332 underscore:

var x = [{"admin\u0332url":"/cca"}];
console.log(x);
console.log(x[0].admin_url);
console.log(x[0].admin\u0332url);
console.log(Object.keys(x[0]))

The Developer Console does not backspace, so a \u0332 underscore looks the same as an ASCII underscore.

The x[0].admin_url would be undefined if you try to access it with a string that uses an ASCII underscore.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
-1

You need to access data property of the response so, it should be,

this.apiService
    .getCCUrls()
    .then(function(data){
        console.log(data.data[0].admin_url);
    });
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • The response (`data`) is an array with one element (object, `[{...}]`). Why should TO use `data.data[0]` when `data` is the array? – Andreas Jun 02 '18 at 11:20
  • 1
    @Andreas i guess OP has posted wrong console , if thats the case it should work when he did data[0].admin_url – Sajeetharan Jun 02 '18 at 11:21
  • 3
    The code given don't contains enough information to support your claim hence this "answer" is just speculation. And if the code doesn't make sense you should probably ask for clarification. – Andreas Jun 02 '18 at 11:25
  • 1
    no clearly if you look at his service you would understand the issue – Sajeetharan Jun 02 '18 at 11:26
  • 1
    The "service" is irrelevant for the problem. The response (`data`) shown is an array of objects and not an object with a `data` property containing an array, hence `data.data[0]` is wrong. – Andreas Jun 02 '18 at 11:30
  • @sajeetharan the first 'data' is stripped out by the last function. but as I mentioned, when I try data[0].admin_url it is just undefined. – mydoglixu Jun 02 '18 at 13:33
  • i dont get it, what is the output you see when you put console.log(data) – Sajeetharan Jun 02 '18 at 13:36
  • the output is the image at the end of my post above – mydoglixu Jun 02 '18 at 13:39
  • oh that is very strange. can you post the json format as comment below? console.log(JSON.stringify(data)); – Sajeetharan Jun 02 '18 at 13:40
  • @sajeetharan- the output of the JSON.stringify is this: [{"agent_url":"/cc/this-other-contact-center","admin_url":"/cca/this-other-contact-center","type":"cc"}] LOL, I don't understand. Shouldn't either data.admin_url or data[0].admin_url just work then? (before stringify) – mydoglixu Jun 02 '18 at 16:45
  • if you are getting that output data[0].admin_url SHOULD DEFINITELY WORK – Sajeetharan Jun 02 '18 at 16:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172313/discussion-between-sajeetharan-and-mydoglixu). – Sajeetharan Jun 02 '18 at 17:09