How to get the title field from a reference field in Sanity.io
67 replies
Last updated: Jul 1, 2021
G
Hello everyone, there’s a way to get the “title field” from a reference field?
The form has a title, but I didn’t figured out how to get the title of a referenced field in preview.
Thanks.
{ name: 'Forms', type: 'object', preview: { prepare: () => ({ title: 'Form Builder', }), }, fields: [ { title: 'Forms', name: 'formBuilder', type: 'reference', to: [{ type: 'form' }], }, ], }
Thanks.
Jul 1, 2021, 9:43 AM
A
Jul 1, 2021, 9:48 AM
A
Preview using fields from referenced documents
Jul 1, 2021, 9:48 AM
G
I’ve tried but I didn’t get the approach
Jul 1, 2021, 9:49 AM
A
You need to use
selectto pick which fields that will become available in the
preparefunction
Jul 1, 2021, 9:52 AM
G
yes I did in this way
preview: {
select: {
title: 'title',
},
prepare: ({ _title_ }) => ({
title: 'Form Builder',
}),
},
Jul 1, 2021, 9:53 AM
G
but the title contains an object with key as reference and not the field itself
Jul 1, 2021, 9:53 AM
A
Sorry,
titleis a reference?
Jul 1, 2021, 9:54 AM
G
is form
Jul 1, 2021, 9:54 AM
G
this is a plceholder
Jul 1, 2021, 9:54 AM
G
but I’ve done in the right way
Jul 1, 2021, 9:54 AM
A
The
If
selectneed to include your reference:
preview: { select: { title: 'title.something' },
titleis a reference, this will follow the reference and look up the field
something.
Jul 1, 2021, 9:54 AM
G
preview: {
select: {
form: 'form',
},
prepare: ({ _form_ }) => {
console.log(_form_);
return {
title: 'Form Builder',
};
},
},
Jul 1, 2021, 9:55 AM
A
You need a dot to follow the reference.
Jul 1, 2021, 9:55 AM
G
form.title?
Jul 1, 2021, 9:55 AM
G
is undefined
Jul 1, 2021, 9:56 AM
A
In your schema, the field is called
formBuilder. So presumably
formBuilder.title.
Jul 1, 2021, 9:56 AM
A
If you paste your whole schema, I can help a little better. Your naming is a bit confusing to me.
Jul 1, 2021, 9:57 AM
G
is undefined
Jul 1, 2021, 9:56 AM
A
You need to use
selectto pick which fields that will become available in the
preparefunction
Jul 1, 2021, 9:52 AM
G
yes I did in this way
preview: {
select: {
title: 'title',
},
prepare: ({ _title_ }) => ({
title: 'Form Builder',
}),
},
Jul 1, 2021, 9:53 AM
G
but the title contains an object with key as reference and not the field itself
Jul 1, 2021, 9:53 AM
A
Sorry,
titleis a reference?
Jul 1, 2021, 9:54 AM
G
is form
Jul 1, 2021, 9:54 AM
G
this is a plceholder
Jul 1, 2021, 9:54 AM
G
but I’ve done in the right way
Jul 1, 2021, 9:54 AM
G
ok now works 🙂 thanks you!’cause you are here I can ask you how to fetch the reference from groq? I mean I have this query
where the result is
*[_type == "page" && slug.current == 'test'] [0] {
"form": builder[29]
}
where the result is
Jul 1, 2021, 10:01 AM
A
The
If
selectneed to include your reference:
preview: { select: { title: 'title.something' },
titleis a reference, this will follow the reference and look up the field
something.
Jul 1, 2021, 9:54 AM
G
preview: {
select: {
form: 'form',
},
prepare: ({ _form_ }) => {
console.log(_form_);
return {
title: 'Form Builder',
};
},
},
Jul 1, 2021, 9:55 AM
A
You need a dot to follow the reference.
Jul 1, 2021, 9:55 AM
G
I can’t find a way in the docs to retrive this field directly from groq query
Jul 1, 2021, 10:01 AM
G
form.title?
Jul 1, 2021, 9:55 AM
G
i’ve tried to add ->
Jul 1, 2021, 10:06 AM
G
to follow the reference but the result was empty
Jul 1, 2021, 10:06 AM
G
ok I’ve got 🙂
Jul 1, 2021, 10:10 AM
A
You figured it out?
Jul 1, 2021, 10:15 AM
A
formBuilder->
Jul 1, 2021, 10:15 AM
G
yes, I think the hard part is to get in the array
Jul 1, 2021, 10:16 AM
G
cause builder is an array
Jul 1, 2021, 10:16 AM
A
formBuilder[]->
Jul 1, 2021, 10:16 AM
G
and there’s different components in,
Jul 1, 2021, 10:16 AM
G
no I mean the builder of page
Jul 1, 2021, 10:17 AM
G
*[_type == "page" && slug.current == 'test'] [0] { builder }
Jul 1, 2021, 10:17 AM
G
builder is an array of a lots of different compoents
Jul 1, 2021, 10:17 AM
G
where inside it could be a form that use a reference
Jul 1, 2021, 10:17 AM
A
I don’t have your whole schema, but something like:
*[_type == "page" && slug.current == 'test'] [0] { "builder": builder[]-> }
Jul 1, 2021, 10:18 AM
A
Depeds on what the
builderfield is
Jul 1, 2021, 10:18 AM
G
as you can see countdown has not reference
Jul 1, 2021, 10:18 AM
G
form has reference
Jul 1, 2021, 10:18 AM
A
What is the root field called?
Jul 1, 2021, 10:18 AM
A
You can do
theFieldName[_type == "reference"]->
Jul 1, 2021, 10:19 AM
G
where can I do this?
Jul 1, 2021, 10:19 AM
G
inside builder?
Jul 1, 2021, 10:19 AM
G
G
as you can see here the entire builder array, and the last element in this case is the form with reference
Jul 1, 2021, 10:24 AM
A
Yes.
*[_type == "page" && slug.current == 'test'] [0] { "form": builder[_type == "reference"] }
Jul 1, 2021, 10:26 AM
G
ok but this exlude the result from the array
Jul 1, 2021, 10:26 AM
G
it should be part of the array
Jul 1, 2021, 10:26 AM
G
my solution is to handle this in front-end
Jul 1, 2021, 10:32 AM
G
don’t know how to do this in groq, we have to do a kind of map of each field and if there’s a reference I have to replace with the referenced
Jul 1, 2021, 10:33 AM
G
uhm
Jul 1, 2021, 10:33 AM
A
You want to have the whole array, but expand references? Try this:
*[_type == "page" && slug.current == 'test'] [0] { "form": builder[] | { ..., "formBuilder": formBuilder-> } }
Jul 1, 2021, 10:34 AM
G
let me try
Jul 1, 2021, 10:39 AM
G
wow
Jul 1, 2021, 10:39 AM
G
the power of groq is unbelievable
Jul 1, 2021, 10:40 AM
G
thanks for your time buddy
Jul 1, 2021, 10:44 AM
G
you saved my day
Jul 1, 2021, 10:44 AM
A
My pleasure!
Jul 1, 2021, 10:51 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.