Adding document reference to user in Sanity client create and patch

11 replies
Last updated: Jan 28, 2026
Hi guys, Im trying to get a handle on how to use the @sanity/client to create a document and also update the user document with the reference to this document. So far I’ve managed to create the document however getting the client to add in the ref to this document id is proving a little difficult.
Here is my code trying to do the following, taking the id after the document is created and patching the user document:

const _addCalc_ = _async_ ()
=>
{

_return await getClient_(_false_)

_.create_({

..._calcDetails_,

__type_:
"calculator",

})

_.then_((_res_)
=> _addRefToUser_(_res_));

};

_addCalc_();


const _addRefToUser_ = _async_ (_res_)
=>
{

_await getClient_(_false_)

_.patch_("user.cf26c3dd-7c63-4fcc-b936-6069b05213a7")

_.set_({ _calculator_: _res._id_ })

_.commit_()

_.then_((_res_)
=> _console.log_(_res_));

};

}
I am able to have the document update the user detail but its giving me an error on the type as its showing as a string. I’m sure I’m missing a better way of doing this process, any help would be appreciated.
AI Update

Looking at your code, the issue is that when you use .set() with a reference field, you need to provide a proper reference object structure, not just the ID as a string. Here's how to fix it:

const addRefToUser = async (res) => {
  await getClient(false)
    .patch("user.cf26c3dd-7c63-4fcc-b936-6069b05213a7")
    .set({ 
      calculator: {
        _type: 'reference',
        _ref: res._id
      }
    })
    .commit()
    .then((res) => console.log(res));
};

The key change is structuring the reference as an object with _type: 'reference' and _ref: res._id instead of just passing res._id directly. This is the proper structure for strong references in Sanity.

However, if your schema defines calculator as an array of references (which is common), you'll want to use the append operation instead:

const addRefToUser = async (res) => {
  await getClient(false)
    .patch("user.cf26c3dd-7c63-4fcc-b936-6069b05213a7")
    .setIfMissing({ calculator: [] })
    .append('calculator', [{
      _type: 'reference',
      _ref: res._id,
      _key: res._id // good practice for array items
    }])
    .commit()
    .then((res) => console.log(res));
};

The patches documentation explains how different operations work. The setIfMissing ensures the array exists before appending, and append adds the new reference to the array.

Also, a small optimization - you can simplify your _addCalc_ function since .then() already returns the result:

const _addCalc_ = async () => {
  const res = await getClient(false).create({
    ...calcDetails,
    _type: "calculator",
  });
  return addRefToUser(res);
};

This approach is cleaner and handles the async flow more naturally than mixing await with .then().

Show original thread
11 replies

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?