简体   繁体   English

尝试将 csv 文件数据导入 Firestore

[英]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));
  1. Download and install Firefoo .下载并安装Firefoo

  2. sign in with Google.使用 Google 登录。 Right-click your project in the left sidebar and choose Import Collections.右键单击左侧边栏中的项目,然后选择 Import Collections。 在此处输入图像描述

  3. Click on the Data File field and select your JSON or CSV file.单击数据文件字段和 select 您的 JSON 或 CSV 文件。

  4. 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.

相关问题 Firestore 导入数据文件未正确保存(ä、ö、ü、á 显示为符号) - Firestore import data-file is not correct saved (ä,ö,ü,á are shown as symbols) 如何使用 excel 或 google 电子表格或 CSV 使用 excel 导入 firebase firestore 数据库中的批量数据 - How to import bulk data in firebase firestore database from excel or google spreadsheet or CSV using flutter 如何将所有 firestore 数据导出到 csv 的 excel? - How export all firestore data to excel of csv? 试图从 Firestore 获取所有收集数据 - Trying to get all the collection data from Firestore 试图从 firestore 获取数据但出现此错误 - trying to get data from firestore but getting this ERROR 尝试在 Firestore 上存储数据时应用程序崩溃 - App crashing while trying to store data on Firestore 如何将数据从 Cloud Firestore 导入到本地模拟器? - How to import data from cloud firestore to the local emulator? 将数据从 GCS 中的文件加载到 GCP firestore - Loading data from a file in GCS to GCP firestore Firestore:Flutter pugin,尝试按字段对数据进行排序时出错 - Firestore: Flutter pugin, error when trying to sort the data by a field 我试图将数据添加到 firestore,但“Firestore 组件不存在。” 终端给出此错误消息 - I was trying to add data to firestore but, "Firestore component is not present." terminal gives this error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM