【问题标题】:CSV file read issue in firebase functionsfirebase 函数中的 CSV 文件读取问题
【发布时间】:2022-02-09 06:45:32
【问题描述】:

我正在尝试在 firebase 函数中读取 csv 文件,以便我可以处理该文件并使用数据执行其余操作。

import * as csv from "csvtojson";
const csvFilePath = "<gdrive shared link>"

try{
    console.log("First Method...")
    csv()
    .fromFile(csvFilePath)
    .then((jsonObj: any)=>{
     console.log("jsonObj....",JSON.stringify(jsonObj));
    })

    console.log("Second Method...")
    const jsonArray=await csv().fromFile(csvFilePath);
    console.log("jsonArray...", JSON.stringify(jsonArray))
         }
catch(e){
    console.log("error",JSON.stringify(e))
}

上述是我尝试读取 csv 的 2 种方法,但都显示 firebase 错误

'错误:文件不存在。检查以确保 csv 的文件路径正确。'

如果是“csvFilePath”,我尝试了 2 种方法

  1. 只是在函数的同一文件夹中添加了csv文件并添加了类似的代码

    const csvFilePath = "./student.csv"
    
  2. 将相同的文件添加到谷歌驱动器并将访问权限更改为任何拥有该链接的人都可以读取和编辑并给出相同的路径

    const csvFilePath = "<gdrive shared link>"
    

两者都显示相同的错误。如果是谷歌驱动器,我不想使用任何类型的谷歌凭据,因为我打算在 firebase 函数中读取一个简单的 csv 文件。

【问题讨论】:

  • 对我来说同样的问题。任何帮助将不胜感激。
  • 嗨!您能否分享对我的回答的反馈,以了解是否需要进一步的帮助?

标签: json firebase csv google-cloud-functions


【解决方案1】:

我将首先建议您在本地或不使用该函数将 csv 转换为 json 并查看它是否有效。这是因为我看到您正在使用 ES6 导入,这可能会导致问题,因为所有 documentation 都使用 require。您也可以尝试CSV Parsethis question 中提供的一些解决方案作为替代方案,在没有该功能的情况下尝试它们以检查它是否真的有效并丢弃它。实际上,从 csv 转换 JSON 后,您就可以上传 JSON,但这取决于您要执行的操作。

我认为实现这一目标的最佳方法是遵循this question 中给出的方法,首先将uploads the file 放入云存储并使用onFinalize() 触发转换。

此外,还将解决与路径类似的这三个问题。他们可以通过添加__dirname 来修复它。每一个都有一些额外的有用信息。

Context for "relative paths" seems to change to the calling module if a module is imported

The csvtojson converter ignores my file name and just puts undefined

How to avoid the error which throws a csvtojson

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2012-10-22
    相关资源
    最近更新 更多