🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Error in React/Sanity tutorial code, resolved with missing comma in query

6 replies
Last updated: Sep 29, 2021
Hi all, I am starting with Sanity and React to build my portfolio. I am following this tutorial: https://www.youtube.com/watch?v=NO7_jgzVgbc&ab_channel=TraversyMedia However on my console,. I get the 400 error.

This is my code --> I might missed something?

import React, { useState, useEffect } from "react"
import { Link } from "react-router-dom"
import sanityClient from "../client"

export default function Post() {
    //query for information with react hooks
    const [postData, setPost] = useState(null);
    useEffect(() => {
        sanityClient
        .fetch(
            `*[_type == "post"]{
            title
            slug,
            mainImage {
                asset-> {
                    _id,
                    url
                }
            }
        }`
        )
        .then((data) => setPost(data))
        .catch(console.error)
    }, []);
    
    return (
        <main>
            <section className="container mx-auto">
                <h1>Blog Post page</h1>
                adokgr
                <div className="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
                    { postData && postData.map((post, index) => (
                    <article>
                        <Link to={"/post"+ post.slug.current} key={post.slug.current}>
                            <span className="block h-64 relative rounded shadow " key={index}>
                                <img src={post.mainImage.asset.url}/>
                                <span>
                                    <h3>{post.title}</h3>
                                </span>
                            </span>
                        </Link>
                    </article>
                    ))}
                </div>
            </section>
        </main>
    );
}
Sep 29, 2021, 5:10 AM
Hi and welcome to the Sanity community!
It looks like your query is missing a comma after
title
(in the
useEffect()
function). Can you see if adding that in removes the error?
Sep 29, 2021, 5:19 AM
This might still fail until you’ve added a slug and mainImage to your second post. You’re currently checking if
postData
exists but not each property. One approach is optional chaining (e.g.,
post?.title
and
post?.slug.current
).
Sep 29, 2021, 5:21 AM
This might still fail until you’ve added a slug and mainImage to your second post.
Sep 29, 2021, 5:21 AM
hey Geoff, thank you, it is working indeed, and you answered my question of what if I dont want/have an image thank u
Sep 29, 2021, 5:25 AM
hey Geoff, thank you, it is working indeed, and you answered my question of what if I dont want/have an image thank u
Sep 29, 2021, 5:25 AM
Great! Happy to hear that’s working for you.
Sep 29, 2021, 5:29 AM

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?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Hey all - what’s the quickest way to batch delete all documents of a certain type? It looks like the `sanity` cli tool allows...Jan 29, 2021
How to turn PortableText into plain text in Javascript?Dec 7, 2020
Rendering nested block contentJan 18, 2021
In the tutorial, there is samples of what the API returns as JSON. Is there a way to view these ?Jan 11, 2021
Warning: Data for Page Exceeds the Threshold of 128 kBAug 17, 2022
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
How to get the number of nested/children components in a GROQ query.Jan 19, 2024
Issue with fetching data from an array of references and non-references in a groq query.Dec 14, 2023
GraphQL query resolving references on a _raw field in GatsbyMay 17, 2022
GROQ query for getting references from arrays in another array in Sanity.ioApr 9, 2020

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle