// getStaticPaths function
export const getStaticPaths: GetStaticPaths = async () => {
const res = await fetch("http://localhost:5000/products");
const products = await res.json();
const paths = products.map((product: { _id: string }) => ({
params: { productid: product._id },
}));
return { paths, fallback: false };
};
export const getStaticProps: GetStaticProps = async (context) => {
try {
const { params } = context;
const productid = params?.productid as string;
if (!productid) {
throw new Error('Invalid product id');
}
const res = await fetch(`http://localhost:5000/products/${productid}`);
if (!res.ok) {
throw new Error('Failed to fetch product');
}
const product: productInterface = await res.json();
return {
props: {
product: product
},
};
} catch (error) {
console.error('Error fetching product:', error);
return {
notFound: true,
};
}
};
this is an error
SyntaxError: Unexpected non-whitespace character after JSON at position 4 build . Error: Failed to collect page data for /products/[productid]
New contributor
Rihanul Islam Sharif is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.