How to create a new document and set a reference in an array using the JavaScript API.

3 replies
Last updated: Jan 9, 2024
With the javascript API, how do I create a new document and set a reference that is a member of an array? I've tried a lot of things and gotten errors, now it's just failing silently.
I understand that I need to set the _ref to the actual ref of the referred to item and I'm setting a uuid _key.

Both of the following fail.
finalistCategory
is the name of the field and it should get populated, one item in an array, and they should be refs to
awardCategory


 finalistCategory: [
      {
        _id: "6c3c48e1-91b1-44b0-bcdc-640037cfabcd",
        _type: "reference",
        _key: "a18b3d20-fd0c-4aeb-8b72-924bcd90d0e1"
      },
    ],

finalistCategory: [
      {
        awardCategory: {
          _id: "6c3c48e1-91b1-44b0-bcdc-640037cfabcd",
          _type: "reference",  
          _key: uuidv4().toString(),
        },
      },
    ],
I wonder if part of the issue is something to do with I'm using
_sanityClient_._createOrReplace_
and maybe I need to be doing something with
patch
as per https://www.sanity.io/docs/js-client#patchupdate-a-document but patch seems to work only on existant docs....
AI Update

The issue with your code is in the structure of the reference objects. When creating an array of references in Sanity, each reference object should use _ref (not _id) to point to the referenced document. Here's the correct format:

finalistCategory: [
  {
    _type: "reference",
    _ref: "6c3c48e1-91b1-44b0-bcdc-640037cfabcd",  // Use _ref, not _id
    _key: "a18b3d20-fd0c-4aeb-8b72-924bcd90d0e1"
  },
]

Your first example was close but used _id instead of _ref. Your second example had the reference nested inside an awardCategory object, which isn't the correct structure for a reference field.

The correct structure for a reference in Sanity is:

  • _type: Must be "reference"
  • _ref: The document ID you're referencing (not _id)
  • _key: Required for array items (which you're correctly generating)

So your complete createOrReplace call should look like:

client.createOrReplace({
  _id: 'your-document-id',
  _type: 'yourDocumentType',
  finalistCategory: [
    {
      _type: "reference",
      _ref: "6c3c48e1-91b1-44b0-bcdc-640037cfabcd",
      _key: uuidv4().toString(),
    },
  ],
  // ... other fields
})

Important note: As mentioned in the Sanity documentation on strong references, the referenced document (the one with ID 6c3c48e1-91b1-44b0-bcdc-640037cfabcd) must already exist in your dataset before you create this reference, or it must be created in the same mutation transaction. If it doesn't exist, the operation will fail silently or throw a validation error.

You're right that createOrReplace is the correct method here - patch is only for updating existing documents. createOrReplace works perfectly for creating new documents with references.

replace
_id
with
_ref

      {
        _ref: "6c3c48e1-91b1-44b0-bcdc-640037cfabcd",
        _type: "reference",
        _key: "a18b3d20-fd0c-4aeb-8b72-924bcd90d0e1"
      },
Oh, thank you! I'd been bashing my head on this for a while
And I'd just overlooked that, I knew it should be _ref, at least in theory. Thanks again

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?