0

I'm creating a record that should have a reference to another record.

I already created a record that has for RecordName France and for record type Countries. The record I now want to create looks like this:

var operations = container.publicCloudDatabase.newRecordsBatch(); // I'm normally creating many cities at once, newRecordsBatch() also works with only one record.  
operations.create({
    recordName: 'Paris'
    recordType: 'Cities',
    fields: {
        Country: {
            value: 'France'
        }
    }
});
operations.commit().then(function(response) {
    if(response.hasErrors) {
        console.log(response.errors[0]);
    }
});

In the CloudKit Dashboard I have set that Cities to have one reference to Countries using the field Country. However when running the code it returns the server responded with a status of 400 (Bad Request).

I watched the WWDC video and the only thing Apple says about references in CloudKit JS is use a Reference object. I don't know what it is, I guess it's a JSON object but does someone know what are the key/values of this object?

Armand Grillet
  • 3,229
  • 5
  • 30
  • 60

1 Answers1

2

A Reference object is a dictionary with the following keys:

  • recordName: The unique name used to identify the record within a zone. Required.
  • zoneID: Dictionary that identifies a record zone in the database.
  • action: The delete action for the reference object. NONE or DELETE_SELF or VALIDATE. Required.

Example of a good syntax for the Country field:

Country: {
    value: {
        recordName: 'France',
        action: 'DELETE_SELF'
    }
}

More info available in the documentation, pages 68-69.

Armand Grillet
  • 3,229
  • 5
  • 30
  • 60