0

So I am trying to fetch data from this https://fakestoreapi.com/products api but I don't seem to get the results. I don't know whether my procedure is wrong or I am missing something. I am using next.js on top of react with typescript. Kindly assist me. Below is my code to fetch the data.

import React from 'react'

interface Props {
    products: {
        id: number
        title: string
        price: string
        category: string
        description: string
        image: string
    }[]
}

export const getStaticProps = async () => {
    const response = await fetch("https://fakestoreapi.com/products")
    const jsonResponse = await response.json()

    console.log(jsonResponse);
    

    return {
        props: { products: jsonResponse }
    }
}

const FetchProducts = ({ products }: Props) => {
  return (
    <div>
        <h1>fetchProducts</h1>

        {products.map(product => (
            <div key={product.id}>
                {product.title}
            </div>
        ))}
    </div>
  )
}

export default FetchProducts
  • Is `getStaticProps()` invoked anywhere? In your browser's debugging tools, are there any errors on the console? Is the AJAX request made? What is the server's response? When you debug, how specifically is this failing? – David Jun 28 '22 at 16:23
  • Does this gives a response ? console.log(jsonResponse); – Learner Jun 28 '22 at 16:24
  • @David Nothing logs to console when I log the response from the request. And it doesn't even fail too. – Nicholas Darko Brown Jun 28 '22 at 16:27
  • @NicholasDarkoBrown: Then what *does* happen? In your browser's debugging tools, on the network tab, is the AJAX request made? Is it what you expect? What is the server's response? – David Jun 28 '22 at 16:28
  • @David this is the only thing that appears in the row: _devMiddlewareManifest.json with status 200 – Nicholas Darko Brown Jun 28 '22 at 16:34
  • @NicholasDarkoBrown: Are you saying that the AJAX request isn't being made? That would certainly explain why you aren't receiving the expected response. So... Where do you intend to make that AJAX request? Where is `getStaticProps()` invoked in your code? – David Jun 28 '22 at 16:35
  • @David yes, it's not made. I want to make the AJAX request in the same file where I have the functional component, after getting the response then I map over the data and render it on my Home page – Nicholas Darko Brown Jun 28 '22 at 16:40
  • I hope you understand why your question is puzzling.... You don't make a request, yet you are surprised there is no response? – Kokodoko Jun 28 '22 at 16:55
  • @Kokodoko the request is made in the getStaticProps function but I don't get the output when I console it, pardon me for that – Nicholas Darko Brown Jun 28 '22 at 17:00
  • @NicholasDarkoBrown: Isn't `getStaticProps()` [invoked *at build time*](https://nextjs.org/docs/basic-features/data-fetching/get-static-props)? It sounds like this isn't where you want to execute this code in the first place. – David Jun 28 '22 at 17:04
  • @David I am new to Next.js so kindly advise where I could put the **getStaticProps()** for it to work – Nicholas Darko Brown Jun 28 '22 at 17:07
  • Is `FetchProducts` a page component? `getStaticProps`/`getServerSideProps` only works in page components, it won't run in regular components. See [NEXTJS: getServerSideProps not working into components](https://stackoverflow.com/questions/64136025/nextjs-getserversideprops-not-working-into-components). – juliomalves Jul 01 '22 at 22:18
  • 1
    @juliomalves That's how I was able to resolve it, that is by fetching the data in the page components and not the other components folder I created. Thanks for the feedback. – Nicholas Darko Brown Jul 02 '22 at 22:05

0 Answers0