0

I have an HTML table I want to populate with values stored in the Cloudkit Dashboard.

I have successfully retrieved records and populated my tables with them, and the problem I am noticing is that if I give it some time and try to reload the table again some records will not get retrieved. I haven't changed any of my code in the time between, so I'm very puzzled as to how/why this is happening. Below is an example of my code, and the error log:

public-query.js :

records.forEach(function(record) {  
var fields = record.fields;  
var tableActual = "<tr><td>" + record['created'].timestamp + "</td><td>" + fields['placeName'].value + </td></tr>  
                    document.write(tableActual)  

ERROR LOG:

Uncaught (in promise) TypeError: Cannot read property 'timestamp' of  undefined
at public-query.js:49:73
at Array.forEach (native)
at public-query.js:47:29

If I remove for example the <td>" + record['created'].timestamp + "</td> then the error log will say the next <td> is undefined.

To be clear, there is currently a record being retrieved WITH its timestamp correctly, and there were other records that were being retrieved and now aren't, with no change in code.

I've received this error at different times in different parts of line 3 (in the actual code there are 16 fields/<td>) even though I know that those fields are populated in the record, and have also retrieved records with those fields unpopulated.

Any idea what's going on here? Is this a bug with Cloudkit or something I am doing wrong?

SamYoungNY
  • 6,444
  • 6
  • 26
  • 43

1 Answers1

1

If you take a look at the Discussion section at the bottom of the CloudKit Web Services Query Reference, you'll see:

Indexes are updated asynchronously so they are not guaranteed to be current.

It's possible that some records returned in your query are marked as deleted and won't include their fields, so you need to check this before assuming the fields exist and access them. Here is an example response you might see:

{
    "records": [{
        "recordName": "195055b3-1db7-4d75-86f0-d9aef8d8381b",
        "deleted": true
    }, {
        "recordName": "712525a8-eccf-4da7-b2d9-981f120bcc82",
        "deleted": true
    }],
    "total": 2
}

If you check for the deleted attribute on these objects then you can ignore them when processing or rendering the data.

Dave Browning
  • 1,256
  • 7
  • 7