Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Setting initial values for reference fields in Sanity schema.

8 replies
Last updated: Mar 4, 2021
Hello! Is it possible to define an Inital Value for a type: "reference". Let's say for the reference to the author of a post, so in the output the name of the author will be: "". How would I do that?
Mar 4, 2021, 10:35 AM
I don't know about defining it as an initial value, but you could achieve something like this in your query:
*[_type == "post"]{
  ...,
  "author": select(
    defined(authorRef) => authorRef -> name,
    ""
  )
}
Mar 4, 2021, 11:18 AM
Well, it should be defined in the schema, because if there is no value set in the studio, there is no author in the query and this will give an arror of undefined.
Mar 4, 2021, 12:02 PM
That's what I mean - the query above would skit around that by always returning something for the author, even if none is set.
Mar 4, 2021, 12:04 PM
For your solution, it feels like maybe you need to create a "default" dummy author to set in your initial value template
Mar 4, 2021, 12:05 PM
Hi Pieter, is this always the same value or should it come from a query? i.e. should it set the reference to a default author? Not sure I fully get the
""
part of the original post šŸ™‚ Thanks for clarifying.
Mar 4, 2021, 12:25 PM
Yes ok, sorry. Let me be more clear about this. My problem is: in Studio leaving the Author (type: "reference") field of a Post empty will cause an error of "undefined" in the output, because there will be no Author object. My question is: Is it possible in a schema to define an empty value as Inital Value for a type: "reference". In this case for the reference to the author of a post, so in the output the name of the author will be: "", instead of "undefined". Or is there another way to counter this problem?
Mar 4, 2021, 12:42 PM
Thanks for explaining! It's possible to set an initial value for a reference field, but that's intended for cases where you want to actually set a default author, not to get an empty string as a query response.
In this situation, I would follow djeglin's suggestion to handle it on the query side instead
or in the front-end code itself.
On the query side, you could use the above suggestion or, alternatively, the
coalesce()
function to set a default value (however, note that an empty string doesn't work here). Example:
// Default values when missing or null in document

*[_type == 'movie']{..., 'rating': coalesce(rating, 'unknown')}
To handle it outside of the query, you could consider checking for the existence of the author object wherever you include it: e.g.
{author && author.name}
.
The same applies if the issue is on the studio side instead, for example in previews. Does this help?
Mar 4, 2021, 12:55 PM
Certainly, thanks Peter, thanks djeglin!
Mar 4, 2021, 1:10 PM

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?