[英]Getting some problem to read the CSV file inside the firebase functions
I am trying to read the csv file inside the Firebase functions so that i can send the mail to the all the records.我正在尝试读取 Firebase 函数中的 csv 文件,以便我可以将邮件发送到所有记录。 I am planning to go with the following procedure
我计划使用以下程序 go
Below is the function下面是function
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);
})
}
});
While running the code i am only getting csv file uploadeded which i have written inside the console.log and then i get the timeout after 1 minute.i am also not getting the The file has been downloaded to log.在运行代码时,我只上传了 csv 文件,我在 console.log 中写入了该文件,然后我在 1 分钟后得到超时。我也没有得到文件已下载到日志。 Can anybody look at the code and help me to get out of this.
任何人都可以查看代码并帮助我摆脱困境。
You are mixing up the use of async/await
together with a call to then()
method.您将
async/await
的使用与对then()
方法的调用混为一谈。 You should also use await
for the fromFile()
method .您还应该对
fromFile()
方法使用await
。
The following should do the trick (untested):以下应该可以解决问题(未经测试):
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) {
//.....
}
});
Also note that (independently of the mixed use of async/await
and then()
), with the following line in your code另请注意(独立于
async/await
和then()
的混合使用),代码中包含以下行
csv().fromFile(tempLocalFile).then(...)
you were not returning the Promise returned by the fromFile()
method.您没有返回
fromFile()
方法返回的 Promise 。 This is a key point in Cloud Functions.这是 Cloud Functions 中的一个关键点。
I would suggest you watch the official Video Series on Cloud Functions ( https://firebase.google.com/docs/functions/video-series/ ) and in particular the videos on Promises titled "Learn JavaScript Promises".我建议您观看有关 Cloud Functions 的官方视频系列 ( https://firebase.google.com/docs/functions/video-series/ ),尤其是标题为“Learn JavaScript Promises”的 Promises 视频。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.