Understanding how to add and modify fields in Sanity documents
31 replies
Last updated: Feb 25, 2022
J
The query I got is not equal to the schema I created, it lacks "grade".
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"
}
],
}
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
J
Then here's the query I wrote:
Feb 24, 2022, 6:12 PM
J
When I console.log the query, it gives me this:
Feb 24, 2022, 6:13 PM
J
The result in the console doesn't include grade
Feb 24, 2022, 6:13 PM
J
why?
Feb 24, 2022, 6:13 PM
J
oh I see. Then would the grade attribute be created as well?
Feb 24, 2022, 6:13 PM
J
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
J
How do I affect the existing document in client?
Feb 24, 2022, 6:15 PM
J
Also, in the backend, it does have grade
Feb 24, 2022, 6:15 PM
J
The studio has grade but the printed out user doesn't have grade
Feb 24, 2022, 6:16 PM
J
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:
It
would give you a
To retroactive apply information to existing documents, you have to run a
mutation either via the API or a client .
If you specifically said in your query:
*[_type == 'user' //other criteria]{ ..., grade }
would give you a
nullfor 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
J
Lemme try publishing it again
Feb 24, 2022, 6:19 PM
J
Oh you are right!
Feb 24, 2022, 6:21 PM
J
got the grade now
Feb 24, 2022, 6:21 PM
J
man
Feb 24, 2022, 6:21 PM
J
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
J
Cool, thankx!
Feb 24, 2022, 6:23 PM
J
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
J
I want every user able to set their grade in frontend
Feb 24, 2022, 6:39 PM
J
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
J
I am using react. If I can do client.create, then I can also do client.patch right?
Feb 24, 2022, 6:46 PM
J
I did the client.create here
Feb 24, 2022, 6:48 PM
J
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
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.
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) })
Feb 24, 2022, 7:03 PM
J
lol dude, it worked! Thank you
Feb 25, 2022, 1:46 AM
J
as simple as this
Feb 25, 2022, 1:46 AM
J
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.