Inconsistent output of Pages related object causes errors in front-end data call.
19 replies
Last updated: May 14, 2020
B
i have a document called
i'm finding inconsistency with the output of Pages if no related items are chosen. On some pages, it simply says
if i console the object
Pagethat has an object called
relatedwhere you choose 3 related pages, this maps to a field called
relatedChoiceson the front-end i am calling for this data with
pageData.related.relatedChoices.
i'm finding inconsistency with the output of Pages if no related items are chosen. On some pages, it simply says
relatedChoices length is 0... but i have 2 pages that for some reason error out and say
pageData.related is null.
if i console the object
pageData.relatedit does show null... why? why are some ok to pass to the child field of relatedChoices, but some stop at the object related?
May 14, 2020, 2:03 PM
K
Hey
Maybe test that out. But I think Ive ran into that. I normally check on the front-end by checking that it exists and has a length
user N
! My guess is that if the length is 0 (empty array/obj) then you at one point had chosen a related page in Sanity. And if its null, then you have never chosen a related page.Maybe test that out. But I think Ive ran into that. I normally check on the front-end by checking that it exists and has a length
May 14, 2020, 2:11 PM
B
yes, that was my thought too
May 14, 2020, 2:13 PM
B
i "think" i just solved it... i think i was running my ternary incorrectly
i was checking if it was undefined before checking for
i'm rebuilding now to make sure
i was checking if it was undefined before checking for
!== null
i'm rebuilding now to make sure
May 14, 2020, 2:14 PM
B
yeah, its something with the way i'm trying to write my check
i'm trying to conditionally load the
if i only check for
i'm trying to conditionally load the
relatedcomponent only if there are
relatedChoices
if i only check for
!== nullit works as i would expect, but as soon as I add a check for undefined or length = 0, it bombs back to the
pageData.related is null
May 14, 2020, 2:16 PM
K
interesting. Does your ternary look something like
pageData && pageData.related && pageData.related.length && (<Component />)
May 14, 2020, 2:19 PM
B
i was going with
||rather than
&&
May 14, 2020, 2:20 PM
B
{pageData.related !== null || !pageData.related === 'undefined' || !pageData.related.relatedChoices.length === 0 ? <Related _related_={pageData.related} /> : null}
May 14, 2020, 2:21 PM
B
yeah, switching to
I don't know why...
🤦‍♂️
&&makes it work ... i seem to always get these 2 mixed up,
AND vs OR
I don't know why...
🤦‍♂️
May 14, 2020, 2:23 PM
B
well, lol, except now it doesn't load on pages where it SHOULD load the component
May 14, 2020, 2:24 PM
P
That’s probably because you should change
===to
>in this case? 🙂
May 14, 2020, 2:26 PM
B
for length?
May 14, 2020, 2:26 PM
P
{pageData.related !== null && pageData.related !== undefined && pageData.related.relatedChoices.length > 0 ? <Related related={pageData.related} /> : null}
May 14, 2020, 2:27 PM
B
ok that finally worked... i think i had the check for undefined incorrect as well
May 14, 2020, 2:28 PM
B
is there a better way of writing this?
May 14, 2020, 2:28 PM
P
You could try this:
{pageData.related && pageData.related.relatedChoices.length > 0 ? <Related related={pageData.related} /> : null}
May 14, 2020, 2:29 PM
B
that first check looks at both
nulland
undefined?
May 14, 2020, 2:29 PM
B
as i've seen both returned
May 14, 2020, 2:30 PM
P
They’re both so-called falsy values, so just using the
•
•
&&operator should cover both of them, and a bunch more:•
false•
0(zero)• `''`/`""` (empty string)
•
null•
undefined•
NaNBut not:
•
'0'(zero in a string)•
'false'(false in a string)•
[](empty array)•
{}(empty object)•
function(){}(empty function)
May 14, 2020, 2:43 PM
K
In your last example,
user M
, would you need to check that pageData.related.relatedChoicesexits before checking the length?
length()wont exist if
relatedChoicesdoesn’t exist right? I could be wrong, just feel like I’ve run into that
May 14, 2020, 3:08 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.