✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Fetch Error - Cannot Read Property 'Map' of Null

3 replies
Last updated: Jun 10, 2021
I'm having problem with my fetch and I'm trying to figure out what the problem is. I get 'cannot read property 'map' of null, and at least I know that I query right because I get the right items when I test in the Vision view. So that's not the problem.This is my code where I just tried to map anything at all:
Jun 10, 2021, 3:07 PM
I would guess that you're trying to access
category
before the fetch is done.
category
is null, and your code will run top to bottom without waiting on the previous item (as JS is aync). So the fetch fires, but the rest is run immediately after while fetch is still waiting.
Before your return you can do something like
if ( category == null ) { return 'Loading...' }
Once the fetch finishes and the category is no longer null, the main return should then render.
Jun 10, 2021, 3:14 PM
Hello, try
{category?.map(...)}
and probably you need
&&
before
references(...)
in the query
Jun 10, 2021, 3:14 PM
Thank you 🎯Amazed by the helpful attitude here
🤗
Jun 10, 2021, 3:19 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?