【问题标题】:errorMessage: "Error: ENOENT: no such file or directory, open '/var/task/credentials.json'" errorType: "Runtime.UnhandledPromiseRejection"errorMessage:“错误:ENOENT:没有这样的文件或目录,打开'/var/task/credentials.json'” errorType:“Runtime.UnhandledPromiseRejection”
【发布时间】:2022-01-25 10:01:30
【问题描述】:

我正在使用 google Sheet api 为个人项目开发 Next JS 应用程序。现在这在我的本地工作完全正常。但是当我将它部署到 Netlify 时,会弹出这个错误。

有人可以提出解决方案吗? 谢谢!

这是我的代码:

import { google } from "googleapis";

export default async function form1(req, res){

    if (req.method === 'POST') {
        // Process a POST request
        console.log("POST hit")
        const auth = new google.auth.GoogleAuth({
            keyFile: "credentials.json",
            scopes: "https://www.googleapis.com/auth/spreadsheets",
        })

        //create client instancce
    const client = await auth.getClient();

    //instance of google sheet api

    const googleSheets = google.sheets({version: "v4", auth: client});


    const spreadsheetId = "*********55653"

    //Write rows TO sheet
    const {name, email, phone} = req.body;

    await googleSheets.spreadsheets.values.append({
        auth,
        spreadsheetId,
        range:"Subs!A:B",
        valueInputOption: "USER_ENTERED",
        resource: {
            values: [
                [name, email, phone]
            ],
        }
    })

    res.send("Success")
      } else {
        // Handle any other HTTP method
        res.status(405)
      }


}

【问题讨论】:

  • 你是否在 gitignore 中添加了 credentials.json 文件。检查一次文件是否存在于 netlify 中

标签: node.js google-api google-oauth google-sheets-api google-api-nodejs-client


【解决方案1】:

credentials.json 是包含有关您的 google 项目的所有信息的文件。没有它,您的应用程序将无法运行。

错误信息no such file or directory, open '/var/task/credentials.json'"表示您的应用程序在该目录中找不到json密钥文件。

确保此文件已上传到您的服务器。

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2019-01-26
    • 2021-06-08
    • 2019-06-24
    • 2016-07-05
    • 2013-02-04
    相关资源
    最近更新 更多