1

I am trying to read a csv file in a firebase function so that I can process the file and do the rest operations using the data.

import * as csv from "csvtojson";
const csvFilePath = "<gdrive shared link>"

try{
    console.log("First Method...")
    csv()
    .fromFile(csvFilePath)
    .then((jsonObj: any)=>{
     console.log("jsonObj....",JSON.stringify(jsonObj));
    })

    console.log("Second Method...")
    const jsonArray=await csv().fromFile(csvFilePath);
    console.log("jsonArray...", JSON.stringify(jsonArray))
         }
catch(e){
    console.log("error",JSON.stringify(e))
}

The above mentioned are the 2 methods I have tried for reading the csv but both shows the firebase error

'Error: File does not exist. Check to make sure the file path to your csv is correct.'

In case of 'csvFilePath' I have tried 2 methods

  1. Just added the csv file in same folder of the function and added the code like

    const csvFilePath = "./student.csv"
    
  2. Added the same file to google drive and changed the access permissions to anyone with the link can read and edit and given the path to same

    const csvFilePath = "<gdrive shared link>"
    

Both shows the same error. In case of google drive I don't want to use any sort of google credential because I was intented to read a simple csv file in firebase function.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Athulya Ratheesh
  • 291
  • 4
  • 22

1 Answers1

0

I will start by proposing that you convert your csv to json locally or without the function and see if it works. This is because I see you are using ES6 imports which might be causing an issue since all the documentation uses require. You can also try CSV Parse or some solutions provided in this question as an alternative, trying them without the function to check if it actually works and discard it. Actually, you can upload the JSON once you have converted it from the csv, but that depends on what you are trying to do.

I think the best way to achieve this, is following the approach given in this question, that first uploads the file into cloud storage and using onFinalize() to trigger the conversion.

Also, will address these three questions that went through similar issues with the path. They were able to fix it by adding __dirname. Each one has some extra useful information.

Context for "relative paths" seems to change to the calling module if a module is imported

The csvtojson converter ignores my file name and just puts undefined

How to avoid the error which throws a csvtojson

Alex
  • 778
  • 1
  • 15