Unlock seamless workflows and faster delivery with our latest releases- get the details

Understanding how to add and modify fields in Sanity documents

31 replies
Last updated: Feb 25, 2022
The query I got is not equal to the schema I created, it lacks "grade".
export default {
    name: 'user',
    title: 'User',
    type:'document',
    fields:[
        {
            name:"userName",
            title:"UserName",
            type:"string"
        },
        {
            name:'image',
            title: 'Image' ,
            type:'string'
        },
        {
            name:"admin",
            title:"Admin",
            type:"boolean",
            initialValue: false,
        },
        {
            name:"grade",
            title:"Grade",
            type:"string",
            initialValue: "somethingpls"
        }
    ],
}
Feb 24, 2022, 6:11 PM
Then here's the query I wrote:
Feb 24, 2022, 6:12 PM
When I console.log the query, it gives me this:
Feb 24, 2022, 6:13 PM
The result in the console doesn't include grade
Feb 24, 2022, 6:13 PM
why?
Feb 24, 2022, 6:13 PM
oh I see. Then would the grade attribute be created as well?
Feb 24, 2022, 6:13 PM
at least it should give grade: undefined
Feb 24, 2022, 6:14 PM
No, because the Content Lake is schemaless, so grade doesn't exist until you set it.
Feb 24, 2022, 6:14 PM
How do I affect the existing document in client?
Feb 24, 2022, 6:15 PM
Also, in the backend, it does have grade
Feb 24, 2022, 6:15 PM
The studio has grade but the printed out user doesn't have grade
Feb 24, 2022, 6:16 PM
Even after I gave a value to the grade manually, it still doesn't have it, so I am confused, is the process of fetching wrong, or the document is wrong
Feb 24, 2022, 6:17 PM
As I said, the backend it schemaless. That field only exists in the Studio UI until you set it.
If you specifically said in your query:

*[_type == 'user' //other criteria]{
  ...,
  grade
}
It
would give you a
null
for grade.
To retroactive apply information to existing documents, you have to run a
mutation either via the API or a client .
Feb 24, 2022, 6:17 PM
One thing to check if you have set the field and it's not coming through on your frontend is whether or not you've published the change.
Feb 24, 2022, 6:19 PM
Lemme try publishing it again
Feb 24, 2022, 6:19 PM
Oh you are right!
Feb 24, 2022, 6:21 PM
got the grade now
Feb 24, 2022, 6:21 PM
man
Feb 24, 2022, 6:21 PM
Thx, it's solved, now I just need to learn how to change it
Feb 24, 2022, 6:22 PM
Come back if you're having trouble with the mutation. It's tricky when you first encounter it!
Feb 24, 2022, 6:22 PM
Cool, thankx!
Feb 24, 2022, 6:23 PM
Hey, I have trouble reading the doc of mutation. I don't know where this example is supposed to belong to:
Feb 24, 2022, 6:39 PM
I want every user able to set their grade in frontend
Feb 24, 2022, 6:39 PM
I know how to write a query like this, but I don't know where that example should be put at. It looks more like a console command.
Feb 24, 2022, 6:41 PM
Ah, if you're writing to your dataset from the frontend you'll need to use one of the clients. What frontend framework are you using?
Feb 24, 2022, 6:43 PM
I am using react. If I can do client.create, then I can also do client.patch right?
Feb 24, 2022, 6:46 PM
I did the client.create here
Feb 24, 2022, 6:48 PM
If I want to change value instead of creating, I can just use client.patch right? I didn't know this function before
Feb 24, 2022, 6:49 PM
Exactly. You'll want to use
patch
. If you look at this example:
client
  .patch('bike-123') // Document ID to patch
  .set({inStock: false}) // Shallow merge
  .commit() // Perform the patch and return a promise
  .then((updatedBike) => {
    console.log('Hurray, the bike is updated! New document:')
    console.log(updatedBike)
  })
  .catch((err) => {
    console.error('Oh no, the update failed: ', err.message)
  })
First, you give it the document ID you want to patch, then you set and object that matches the fields in your schema you want to change, then you commit those changes.
Feb 24, 2022, 7:03 PM
lol dude, it worked! Thank you
Feb 25, 2022, 1:46 AM
as simple as this
Feb 25, 2022, 1:46 AM
one time success no debug lol
Feb 25, 2022, 1:46 AM

Sanity– build remarkable experiences at scale

Sanity is a 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.

Was this answer helpful?