Issue with LiveEdit causing page to break with "TypeError: Cannot read properties of null (reading ‘_id’)"
4 replies
Last updated: Jun 2, 2023
R
hey everyone! I have a question… so I’ve included LiveEdit: true to my schema docs which is working really well, but I’m noticing this frustrating issue. When I add a piece to a works reference schema and don’t save or publish it, my page breaks with this error “TypeError: Cannot read properties of null (reading ‘_id’)” even though I have a truthy “&&” condition… What’s my error?
Would appreciate any insight, thank u in advance
❤️
Would appreciate any insight, thank u in advance
❤️
Jun 2, 2023, 1:46 AM
R
This is the page on my frontend built w Next.js
{works &&
works.map(
(
{
_id=“”,
year = “”,
title = “”,
category = “”,
dimensions = “”,
status = “”,
archivestatus = “”,
archiveimages =“”, },
index ) => (
<div
key={index}> <Accordionitem
year={year}
title={title}
category={category}
dimensions={dimensions}
status={status}
archivestatus={archivestatus}
archiveimages={archiveimages} />
</div>
)
)}
{works &&
works.map(
(
{
_id=“”,
year = “”,
title = “”,
category = “”,
dimensions = “”,
status = “”,
archivestatus = “”,
archiveimages =“”, },
index ) => (
<div
key={index}> <Accordionitem
year={year}
title={title}
category={category}
dimensions={dimensions}
status={status}
archivestatus={archivestatus}
archiveimages={archiveimages} />
</div>
)
)}
Jun 2, 2023, 1:46 AM
C
Hi
What I'm thinking, then, is about how the behavior of Sanity references requires you to publish any new referenced item you create, before its reference can be valid.
I'll give a doc page below you can read, though I have to wonder about how this interacts with LiveEdit, and thus whether using weak references -- or possibly a test in that mapping function you are using for the presenceof an _id -- might be able to help you.
It depends on how Sanity operates for the conditions, and in whether you really want to use LiveEdit in this situation.
A practical thought might be to avoid the circumstance by design, and simply require a Publish, which would update the website immediately given you are also using a form of SSR there.
Here's the reference, holding the thought that you find a nice and solid solution around this...!
Clive
https://www.sanity.io/docs/reference-type
user Q
, and a first suggestion might be that the error isn't coming from that section of code -- it's a Javascript error, and you aren't using _id there, it seems.What I'm thinking, then, is about how the behavior of Sanity references requires you to publish any new referenced item you create, before its reference can be valid.
I'll give a doc page below you can read, though I have to wonder about how this interacts with LiveEdit, and thus whether using weak references -- or possibly a test in that mapping function you are using for the presenceof an _id -- might be able to help you.
It depends on how Sanity operates for the conditions, and in whether you really want to use LiveEdit in this situation.
A practical thought might be to avoid the circumstance by design, and simply require a Publish, which would update the website immediately given you are also using a form of SSR there.
Here's the reference, holding the thought that you find a nice and solid solution around this...!
Clive
https://www.sanity.io/docs/reference-type
Jun 2, 2023, 3:27 AM
R
Ah.. Clive thank you so much for your long reply and suggestion!! I’ve disabled LiveEdit on pages with arrays of references and it’s all working ok now, makes a lot of sense now in retrospect why this isn’t the best idea… I’m going to keep LiveEdit on for more simple content types. But disabling it for now on the other pages has helped me a lot.
Appreciate your eye : )
Appreciate your eye : )
Jun 2, 2023, 4:04 AM
C
Most welcome, Ritu -- glad you got it going 🙂
Jun 2, 2023, 4:05 AM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.