2

I am trying to get a Repeat Box to fill up with objects from a JSON document. However, it wont add each item but rather multiple copies of the first item in the JSON document. Any clues as to why?

I am using the following code to pull objects from my JSON document.

function Project_WebClient1_OnSyndicationSuccess(e){
      var FactsArray = [];
    var parsedResponse = JSON.parse(this.responseText);
    var numOfFacts = parsedResponse.results.length;
    for (var i = 0; i < numOfFacts; i++) {
        var FactsObject = {};
        FactsObject.heading = parsedResponse.results[i].heading;
        FactsObject.ReleaseDate = parsedResponse.results[i].ReleaseDate;   
        FactsObject.url = parsedResponse.results[i].url;      
        FactsArray.push(FactsObject);
        log(FactsObject.heading);
    }
            Pages.pgFundInfo.RepeatBox1.dataSource = FactsArray;
            Pages.pgFundInfo.RepeatBox1.refresh();
Data.notify("Data.FactSheets_OutDSetFactSheets.FactSheetsId");
}

Here is the JSON Document:

{
    "results": [
        {
            "FactFile": {
                "__type": "File",
                "name": "Sept2015.pdf",
                "url": "http://files.sample.com/Sept2015.pdf"
            },
            "Heading": "Medium Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:29:00.000Z"
            },
            "createdAt": "2015-10-31T06:28:03.189Z",
            "objectId": "XUS4guS8Hu",
            "updatedAt": "2015-11-04T10:00:37.092Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "MedConsGrowthSept2015.pdf",
                "url": "http://files.sample.com/MedConsGrowthSept2015.pdf"
            },
            "Heading": "Medium-Conservative Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:30:00.000Z"
            },
            "createdAt": "2015-10-31T06:31:18.502Z",
            "objectId": "LOLyM2KX5g",
            "updatedAt": "2015-11-04T10:00:40.561Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "HiMedGrowthSept2015.pdf",
                "url": "http://files.sample.com/HiMedGrowthSept2015.pdf"
            },
            "Heading": "High-Medium Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:50:00.000Z"
            },
            "createdAt": "2015-10-31T06:50:54.367Z",
            "objectId": "I59ZKa5Onq",
            "updatedAt": "2015-11-04T10:00:47.268Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "HiGrowthSept2015.pdf",
                "url": "http://files.sample.com/HiGrowthSept2015.pdf"
            },
            "Heading": "High Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:52:00.000Z"
            },
            "createdAt": "2015-10-31T06:52:05.618Z",
            "objectId": "m4vkEDBgRr",
            "updatedAt": "2015-11-04T10:00:52.294Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "ConsGrowthSept2015.pdf",
                "url": "http://files.sample.com/ConsGrowthSept2015.pdf"
            },
            "Heading": "Conservative Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:53:00.000Z"
            },
            "createdAt": "2015-10-31T06:53:27.399Z",
            "objectId": "bthR88Ck8y",
            "updatedAt": "2015-11-04T10:00:55.160Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "PerformerSept2015.pdf",
                "url": "http://files.sample.com/PerformerSept2015.pdf"
            },
            "Heading": "Multi-Asset Class Portfolio Range September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:55:00.000Z"
            },
            "createdAt": "2015-10-31T06:55:32.021Z",
            "objectId": "JluOn1O0IO",
            "updatedAt": "2015-11-04T10:00:58.839Z"
        }
    ]
}

I am getting the correct number of results coming through, however, inside my repeatbox, it lists them but all using the data from the first entry and not the rest.

What am I doing wrong in my for loop for this?

JESlabbert
  • 160
  • 2
  • 13

2 Answers2

2

According to your JSON document, this part of your loop

FactsObject.heading = parsedResponse.results[i].heading;
FactsObject.ReleaseDate = parsedResponse.results[i].ReleaseDate;   
FactsObject.url = parsedResponse.results[i].url;  

should be rewritten as

FactsObject.heading = parsedResponse.results[i].Heading; // notice the capital H
FactsObject.ReleaseDate = parsedResponse.results[i].ReleaseDate; // maybe use the .iso attribute, if you don't want to assign the whole object  
FactsObject.url = parsedResponse.results[i].FactFile.url;  // notice the FactFile     

Otherwise it will get undefined results.

But I'm not sure what's causing the problem of only getting the first item, rather then each individual one.

edit:

If I just run

for (var i = 0, len = parsedResponse.results.length; i < len; i++) {
    console.log(parsedResponse.results[i]);
}

I get each indivual item, not the first one multiple times.

see http://jsfiddle.net/q2burjk3/

Decay42
  • 802
  • 1
  • 9
  • 20
  • Ok, this is definitely almost there. I tried this and I am getting all the objects through on the log, however, my label's text is always defaulting to the first value. So I am guessing that it is something to do with my label's hookup – JESlabbert Nov 05 '15 at 08:54
1

@Decay42's suggested corrections are good.

I don't know what your RepeatBox config looks like. You might need to define an onRowRender function.

See: https://gist.github.com/SmartfaceDocs/5aa40d93fd296a7d2ef9#file-codeblock-js-L27-L29

Also, I'm curious what the log function in the loop does because I don't think there is a global JS log function.

subodh1
  • 46
  • 2
  • Its a Smartface log. It shows me what object is being pulled through in the Smartface interface. – JESlabbert Nov 04 '15 at 13:28
  • @JESLAB what is the result of : console.log(parsedResponse); are you sure to have from `this.responseText` an array on `results` with different items and not the same items repeated all over your array ??? – Anonymous0day Nov 04 '15 at 13:41
  • Okay, from what I can see from the console log, it is finding one object called results. I am guessing it is counting the objects within that object to give my count of 6 but using the first one to render the heading value? – JESlabbert Nov 04 '15 at 13:51
  • can you tell us what come with :`console.log(parsedResponse.results[i].heading);` in your loop just before `var FactsObject = {};` – Anonymous0day Nov 04 '15 at 14:26
  • Ok, the log is showing the correct values now. The problem I think lies with hooking up my label correctly inside the Repeat Box. – JESlabbert Nov 05 '15 at 09:39