Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

432
Views
TypeError: Cannot destructure property 'image' of 'product' as it is null

I am unsure why I have this error. Could you please take a look and advise?

import React from 'react';
import { client, urlFor } from '../../lib/client';

const ProductDetails = ({ product, products }) => {

    const { image, name, details, price } = product; 
    return (
        <div>
            <div className='product-detail-container'>
                <div>
                    <div className='image-container'>
                        <img src={urlFor(image && image[0])} />
                    </div>
                </div>
            </div>
        </div>
    )
}

export const getStaticPaths = async () => {
    const query = `*[_type == "product"] {
        slug {
            current
        }
    }
    `;

    const products = await client.fetch(query);

    const paths = products.map((product) => ({
        params: {
            slug: product.slug.current
        }
    }));

    return {
        paths,
        fallback: 'blocking'
    }
}

export const getStaticProps = async ({ params: { slug }}) => {
    const query = `*[_type == "product" && slug.current == '${slug}'][0]`;
    const productsQuery = '*[_type == "product"]'

    const product = await client.fetch(query);
    const products = await client.fetch(productsQuery);
  
    console.log(product);

    return {
      props: { products, product }
    }
}

export default ProductDetails

error - pages/product/[slug].js (6:12) @ ProductDetails TypeError: Cannot destructure property 'image' of 'product' as it is null. 4 | const ProductDetails = ({ product, products }) => { 5 |

6 | const { image, name, details, price } = product; | ^ 7 | return ( 8 | 9 |

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I saw this tutorial about sanity, if you get this error when you click on the banner product you have to make sure that you have a Product with the slug name that is the same as your "Product" input in the Banner item.

I don't know if my explanations are clear but when you create a Banner item on Sanity you don't create a Product you have to 'link it'

about 4 years ago · Juan Pablo Isaza Report

0

The answer is shown in the provided error-message:

TypeError: Cannot destructure property 'image' of 'product' as it is null.

The product that you use as a prop to instantiate ProductDetails is null.

about 4 years ago · Juan Pablo Isaza Report

0

If the above code is an accurate representation which is hard to tell due to the way the question is laid out then the error is in your queries to sanity.

The getStaticProps and getStaticPaths functions are not querying correctly and returning null instead of your products.

I believe this is as you are missing quotes (backticks if using dynamic queries) around the query as below.

export const getStaticPaths = async () => {
  const query = `*[_type == "product"] {
        slug {
            current
        }
    }`
  const products = await client.fetch(query)
  const paths = products.map((product) => ({
    params: { slug: product.slug.current }
  }))

  return {
    paths,
    fallback: 'blocking'
  }
}
export const getStaticProps = async ({ params: { slug } }) => {
  const query = `*[_type == "product" && slug.current == '${slug}'][0]`
  const product = await client.fetch(query)

  const productsQuery = `*[_type == "product"]`
  const products = await client.fetch(productsQuery)

  return {
    props: { products, product }
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!