简体   繁体   English

在 Next.js 中连接到 Google 电子表格时出错

[英]Getting an error while connecting to Google spreadsheet in Next.js

I am new to Next.js and trying to use google sheet as a database for a project.我是 Next.js 的新手,并试图将 google sheet 用作项目的数据库。 My application is working fine but still displays in display "not forgot to setup environment variable".我的应用程序运行良好,但仍显示“未忘记设置环境变量”。 I have created a .env.local file and I have declared all variables those I required for me, but still getting an error.我创建了一个.env.local文件,并声明了我需要的所有变量,但仍然出现错误。

在此处输入图像描述

Sheet.js

This is sheet.js file where I use google API.这是我使用 google API 的sheet.js文件。

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: process.env.SPREADSHEET_NAME,
    });

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

  return [];
}

index.js

This is main file of index.js where we wrote all code for making UI.这是index.js的主文件,我们在其中编写了所有用于制作 UI 的代码。

import Head from "next/head";
import Image from "next/image";
import styles from "../styles/Home.module.css";

import { getDataFromSheets } from "./api/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.SPREADSHEET_ID);

export async function getStaticProps() {
  const sheet = await getDataFromSheets();
  return {
    props: {
      data: sheet.slice(0, sheet.length), // remove sheet header
    },
    revalidate: 1, // In seconds
  };
}

.env.local

For security purposes I have hidden my API key.为了安全起见,我隐藏了我的 API 密钥。

GOOGLE_SHEETS_PRIVATE_KEY=d38ed59277fcc18c3b6aXXXXXXXXXXXXXX    
GOOGLE_SHEETS_CLIENT_EMAIL=mini-project@miniproject-328105.iam.gserviceaccount.com
SPREADSHEET_ID=https://docs.google.com/spreadsheets/d/1MO8RLXxYN3n_Zp9YTILeCk28T4ueqDba3OjIuMULpMU/edit#gid=0
SPREADSHEET_NAME=sheet

You just need quotes around values in .env.local file.您只需要在 .env.local 文件中为值加上引号。

Right Way正确的路

GOOGLE_SHEETS_PRIVATE_KEY="d38ed59277fcc18c3b6aXXXXXXXXXXXXXX"    
GOOGLE_SHEETS_CLIENT_EMAIL="mini-project@miniproject-328105.iam.gserviceaccount.com"
SPREADSHEET_ID="https://docs.google.com/spreadsheets/d/1MO8RLXxYN3n_Zp9YTILeCk28T4ueqDba3OjIuMULpMU/edit#gid=0"
SPREADSHEET_NAME="sheet"

Wrong Way错误道

GOOGLE_SHEETS_PRIVATE_KEY=d38ed59277fcc18c3b6aXXXXXXXXXXXXXX    
GOOGLE_SHEETS_CLIENT_EMAIL=mini-project@miniproject-328105.iam.gserviceaccount.com
SPREADSHEET_ID=https://docs.google.com/spreadsheets/d/1MO8RLXxYN3n_Zp9YTILeCk28T4ueqDba3OjIuMULpMU/edit#gid=0
SPREADSHEET_NAME=sheet

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Next.js 中使用 pdf-viewer-reactjs 模块时出错 - Getting error while using pdf-viewer-reactjs module in Next.js next.js - 构建时出错:生成 static 页面(0/5)类型错误:无法读取未定义的属性 - next.js - Getting error while building: Generating static pages (0/5)TypeError: Cannot read properties of undefined 使用 google 电子表格作为 Next.JS 网站源的问题:更改不会更新页面 - Problem using google spreadsheet as source for a Next.JS website: changes won't update pages 将 Next.js 与 Jest 一起使用时出现错误 - Getting Error When Using Next.js With Jest 在运行使用solidity 智能合约的next.js 应用程序时,出现“无法读取未定义的属性”错误 - While running my next.js app that is using solidity smart contracts, I am getting "Cannot read properties of undefined" error 将身体背景图像连接到 next.js 中的 state - Connecting body background image to a state in next.js 访问 Next.js 中 map 中的数据 object 的值时出现未定义 - Getting Undefined while accessing the values of data object inside map in Next.js 如何在使用顺风 css 时在本地将谷歌字体添加到 Next.Js 项目 - How to Add a google font to Next.Js Project locally while using tailwind css 在 next.js 中呈现排序数组时出错 - Error with rendering a sorted array in next.js 在 getStaticProps function 中序列化 Next.js 时出错? - Error serializing Next.js in getStaticProps function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM