在Node csv-parse模块中使用Cast选项时出错

时间:2019-01-09 23:51:19

标签: node-csv-parse

我是Node.js新手,我尝试使用csv-parse将csv数据流传输到firestore。

运行脚本时,使用“ cast:true”选项从下面的另一个StackOverflow答案中抄录了脚本,但出现错误“错误:参数“ documentPath”不是有效的资源路径。路径必须是非空字符串”。 (报告于@ google-cloud \ firestore \ build \ src \ validate.js:92)

如果我删除“ cast:true”选项,它将正常工作并传输数据。

以下是csv-parse项目选项文档的链接:https://csv.js.org/parse/options/


    var admin = require("firebase-admin");
    var serviceAccount = require("./serviceAccount.json");
    var fs = require('fs');
    var csvFile = "test.csv"
    var parse = require('csv-parse');
    require('should');

    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://myproject.firebaseio.com"
    });

    var firestore = admin.firestore();
    var thisRef;
    var obj;
    var counter = 0;
    var commitCounter = 0;
    var batches = [];
    var header = false;
    batches[commitCounter] = firestore.batch();

    fs.createReadStream(csvFile)
        .pipe(
          parse({delimiter: ',',cast:true})
        )
        .on('data', function(csvrow) {
            if(header == false){
                header = csvrow;
            } else if(counter <= 498){
                obj = {};
                for (var  i = 2, leni = csvrow.length-1; i < leni; i++){
                    obj[header[i]] = csvrow[i];
                }
                thisRef = firestore.collection("parcels").doc(csvrow[1]);
                batches[commitCounter].set(thisRef, obj);
                counter = counter + 1;          
          } else {
              counter = 0;
              commitCounter = commitCounter + 1;
              batches[commitCounter] = firestore.batch();
          }
        })
        .on('end',function() {
          writeToDb(batches);
        });

    function oneSecond() {
      return new Promise(resolve => {
          setTimeout(() => {
              resolve('resolved');
          }, 5000);
      });
    }

    async function writeToDb(arr) {
      console.log("beginning write");
      for (var i = 0; i < arr.length; i++) {
          await oneSecond();
          arr[i].commit().then(function () {
              console.log("wrote batch " + i);
          });
      }
      console.log("done.");
    }

0 个答案:

没有答案