javascriptcloudkitcloudkit-web-services

Creating a reference with CloudKit JS


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?


Solution

  • A Reference object is a dictionary with the following keys:

    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.