1

I have a lightning component with this attribute:

<aura:attribute name="transportData" type="Object[]" access="global"/>

I am getting the data from a http Request:

$.ajax({ 
        url: "https://xxxx/api/transport/",
        type: "GET", 
        beforeSend: function(xhr){
            xhr.setRequestHeader("Authorization", "Basic XXXXX");
        }, 
        crossDomain: true,
        success: function(data){
           console.log(data); 
           component.set("v.transportData", data);
        }
});

The console.log gives me data like this example:

{client: "Client01", MTA: "MTA1", Id: 3713, current: 1632679, total: 1845573, …}

In another function I want to get the data from the lightning attribute back and store it in a variable:

var transport = component.get("v.transportData");

But I am always getting an empty array with length of 0.

Funnily enough when I want to get the data in markup, it is showing right:

<aura:iteration items="{!v.transportData}" var="obj">
<span>{!obj.client}</span>  //=Client01
</aura:iteration>

Could you tell me what I am doing wrong?

4
  • 2
    The data you show as an "example" is not an Array. Are you expecting one?
    – sfdcfox
    Dec 14, 2017 at 15:25
  • Could you add more code here ? Also why not use apex to make the API callout than directly from client site ? Dec 14, 2017 at 15:32
  • @sfdcfox yes it is an array because the aura:iteration is showing more than one value, when the callout gets multiple values for "client". It is just not working when I want to get the attribute Object[] back in the javascript controller with component.get("v.transportData") Dec 15, 2017 at 6:44
  • @MohithShrivastava I don't need an apex controller, I need the callout data just in the component and not in a database. Dec 15, 2017 at 6:46

0

You must log in to answer this question.