Not displaying data in next js?

Viewed 400

I'm new in next JS, when I am trying to display my data in a particular route , its not showing , can anyone tell me why its not showing, if you have any query regarding my question please free fell to ask.

Folder Structure

Output as an Empty Route

index.js

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

 import { getDataFromSheets } from "../../libs/sheets"
    
    export default function handler(req, res) {
    
      const { item, description } = getDataFromSheets();
    
      res.status(200).json({ item, description })
      
    }

sheet.js

This is the sheet.js file where i use google api and google sheet id and email that required for making application. GetdataFromSHeet method i called in a index.js file

import { google } from 'googleapis';

export async function getDataFromSheets() {
  try {
    const target = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
    const jwt = new google.auth.JWT(
      process.env.GOOGLE_SHEETS_CLIENT_EMAIL,
      null,
      (process.env.GOOGLE_SHEETS_PRIVATE_KEY || '').replace(/\\n/g, '\n'),
      target
    );

    const sheets = google.sheets({ version: 'v4', auth: jwt });
    const response = await sheets.spreadsheets.values.get({
      spreadsheetId: process.env.SPREADSHEET_ID,
      range: 'sheet'
    });

    
    const rows = response.data.values;
    if (rows.length) {
      return rows.map((row) => ({
        title: row[0],
        description: row[1],
      }));
    }
  } catch (err) {
    console.log(err);
  }
  return [];
}
0 Answers
Related