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 the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?