am new in next JS , I want to display my index.js file data in dynamicid.js file in JSON format , can anyone tell me how can I display it.
index.js
This is the index.js file where My output is displayed, but I want to display my output data in particular route in JSON Format.
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import { getDataFromSheets} from './libs/sheets'
export default function Home({data}) {
return (
<div className={styles.container}>
<Head>
<title>Nextsheet 💩</title>
<meta
name="description"
content="Connecting NextJS with Google Spreadsheets as Database"
/>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1>Welcome to Nextsheet 💩</h1>
<p>Connecting NextJS with Google Spreadsheets as Database</p>
<ul>
{data && data.length ? (
data.map((item) => (
<li key={item}>
{item.title} - {item.description}
</li>
))
) : (
<li>Error: do not forget to setup your env variables 👇</li>
)}
</ul>
</main>
</div>
)
}
// console.log(process.env.NAME);
export async function getStaticProps(context) {
const sheet = await getDataFromSheets();
return {
props: {
data: sheet.slice(0, sheet.length), // remove sheet header
},
revalidate: 1, // In seconds
};
}
dynamicid.js
This is my route file where I want to display my data in JSON Format.
export default function handler(req, res) {
res.status(200).json(name : "Ashish")
}
the json method on res accepts a javascript object. your current implementation is not a valid JS.
to fix it use the following:
res.status(200).json({name : "Ashish"})