Watch a live product demo 👀 See how Sanity powers richer commerce experiences

How to Update a User Document Using the @Sanity Client

11 replies
Last updated: Aug 2, 2022
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.
Aug 1, 2022, 10:53 PM
Can you try changing to the following?
const addCalc = async () =>
  await getClient(false)
    .create({ ...calcDetails, _type: 'calculator' })
    .then(
      async res =>
        await getClient(false)
          .patch('user.cf26c3dd-7c63-4fcc-b936-6069b05213a7')
          .set({
            calculator: {
              _ref: res._id,
              type: 'reference',
            },
          })
          .commit()
          .then(res => console.log(res))
    );

Aug 1, 2022, 11:10 PM
That seems helpful - but I’m now getting an error ClientError: Key “type” not allowed in ref
Aug 1, 2022, 11:16 PM
What does your calculator schema look like in your user document?
Aug 1, 2022, 11:18 PM
_import_ { Document } _from_ "./schemaTypes";


export
_default_ <Document>{

name: "calculator",

title: "Calculator",

type: "document",

fields: [

{

name: "vehicle",

title: "Vehicle",

type: "string",

},

{

name: "distance",

title: "Distance",

type: "string",

},

{

name: "fuelEfficiency",

title: "Fuel Efficiency",

type: "string",

},

{

name: "eVehicle",

title: "E-Vehicle",

type: "string",

},

{

name: "activeStep",

title: "Active Step",

type: "number",

},

],

};
Aug 1, 2022, 11:19 PM
What does the user document that references this calculator look like?
Aug 1, 2022, 11:44 PM
import { Document } from "./schemaTypes";

export default <Document>{
  name: "user",
  title: "User",
  type: "document",
  fields: [
    {
      name: "name",
      title: "Name",
      type: "string",
    },
    {
      name: "email",
      title: "Email",
      type: "string",
    },
    {
      name: "image",
      title: "Image",
      type: "url",
    },
    {
      name: "calculator",
      title: "Calculator Data",
      type: "reference",
      to: { type: "calculator" },
    },
  ],
};
Aug 1, 2022, 11:53 PM
I thinik I figured it out…need to use _type
Aug 1, 2022, 11:55 PM
thanks for your help!
Aug 1, 2022, 11:55 PM
Fantastic!
Aug 2, 2022, 5:48 AM
Is this more or less the correct way to link the records?
Aug 2, 2022, 3:23 PM
It is!
Aug 2, 2022, 4:42 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing