[英]Trying to import csv file data to Firestore
I am using the code below to upload data to Firestore but am getting the error message below:我正在使用下面的代码将数据上传到 Firestore,但收到以下错误消息:
SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句
on this line of code:在这行代码上:
"import promises from 'fs';" “从'fs'导入承诺;”
import { promises } from 'fs';
const { readFile } = promises;
import { promisify } from 'util';
const parse = promisify(import('csv-parse'));
import { Firestore } from '@google-cloud/firestore';
if (process.argv.length < 3) {
console.error('Please include a path to a csv file');
process.exit(1);
}
const db = new Firestore();
function writeToFirestore(records) {
const batchCommits = [];
let batch = db.batch();
records.forEach((record, i) => {
var docRef = db.collection('firebasetest').doc(record.acctid);
batch.set(docRef, record);
if ((i + 1) % 500 === 0) {
console.log(`Writing record ${i + 1}`);
batchCommits.push(batch.commit());
batch = db.batch();
}
});
batchCommits.push(batch.commit());
return Promise.all(batchCommits);
}
async function importCsv(csvFileName) {
const fileContents = await readFile(csvFileName, 'utf8');
const records = await parse(fileContents, { columns: true });
try {
await writeToFirestore(records);
}
catch (e) {
console.error(e);
process.exit(1);
}
console.log(`Wrote ${records.length} records`);
}
importCsv(process.argv[2]).catch(e => console.error(e));
sign in with Google.使用 Google 登录。 Right-click your project in the left sidebar and choose Import Collections.右键单击左侧边栏中的项目,然后选择 Import Collections。
Click on the Data File field and select your JSON or CSV file.单击数据文件字段和 select 您的 JSON 或 CSV 文件。
That's it.而已。 Click the Import button.单击导入按钮。
Your CSV rows are imported into Firestore and a progress popup opens.您的 CSV 行被导入 Firestore 并打开一个进度弹出窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.