🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

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?