【问题标题】:Getting some problem to read the CSV file inside the firebase functions在 firebase 函数中读取 CSV 文件时遇到问题
【发布时间】:2020-04-05 05:20:58
【问题描述】:

我正在尝试读取 Firebase 函数中的 csv 文件,以便将邮件发送到所有记录。我打算按照以下程序进行

  1. 上传 csv 文件
  2. 触发终结函数
  3. 阅读文件并发送电子邮件

下面是函数

import * as functions from "firebase-functions";
import * as mkdirp from "mkdirp-promise";
import * as os from "os";
import * as path from "path";
import csv = require('csvtojson');

const gcs = require('@google-cloud/storage')({ keyFilename: 'service-account-credentials.json' });
const csvDirectory = "csv";
export = functions.storage.object().onFinalize(async (object) => {
    const filePath = object.name;
    const contentType = object.contentType;
    const fileDir = path.dirname(filePath);

    if(fileDir.startsWith(csvDirectory) && contentType.startsWith("text/csv")) {
        const bucket = gcs.bucket(object.bucket);
        const file = bucket.file(filePath);
        const fileName = path.basename(filePath);
        const tempLocalFile = path.join(os.tmpdir(), filePath);
        const tempLocalDir = path.dirname(tempLocalFile);
        console.log("values", bucket, file, fileName, tempLocalDir, tempLocalFile);
        console.log("csv file uploadedeeeed");
        await mkdirp(tempLocalDir);
        await bucket.file(filePath).download({
           destination: tempLocalFile
        });
        console.log('The file has been downloaded to', tempLocalFile);
        csv()
           .fromFile(tempLocalFile)
           .then((jsonObj) => {
              console.log(jsonObj);
           })
    }
});

在运行代码时,我只上传了我在 console.log 中编写的 csv 文件,然后 1 分钟后我得到了超时。我也没有得到文件已下载到日志。任何人都可以查看代码并帮助我摆脱困境。

【问题讨论】:

  • 你的文件上传到bucket成功了吗?
  • @BadPiggie 是的,我正在从 Firebase 存储中手动上传它
  • 您能否展示您的整个 index.js 文件,而不仅仅是 Cloud Function 的 sn-p。请同时添加导入。
  • @RenaudTarnec 立即查看

标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

您将async/await 的使用与对then() 方法的调用混为一谈。你也应该use await for the fromFile() method

以下应该可以解决问题(未经测试):

export = functions.storage.object().onFinalize(async (object) => {
    const filePath = object.name;
    const contentType = object.contentType;
    const fileDir = path.dirname(filePath);

    try {
            if (fileDir.startsWith(csvDirectory) && contentType.startsWith("text/csv")) {
                //.....
                await mkdirp(tempLocalDir);
                await bucket.file(filePath).download({
                    destination: tempLocalFile
                });
                console.log('The file has been downloaded to', tempLocalFile);

                const jsonObj = await csv().fromFile(tempLocalFile);
                console.log(jsonObj);
                return null;

             } else {
                //E.g. throw an error
             }


    } catch (error) {
        //.....
    } 

});

还要注意(独立于async/awaitthen() 的混合使用),在您的代码中使用以下行

csv().fromFile(tempLocalFile).then(...)

你没有返回 fromFile() 方法返回的 Promise。这是 Cloud Functions 中的一个关键点。

我建议您观看有关 Cloud Functions 的官方视频系列 (https://firebase.google.com/docs/functions/video-series/),尤其是标题为“学习 JavaScript Promises”的 Promises 视频。

【讨论】:

    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2012-09-28
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多