How can i use csvtojson npm package correctly?

Hello, I’m trying to use csvtojson. I use it with my local file it worked. Example:

const csvFile = await csv().fromFile('./cloud/controllers/a.csv')

If i want to do that with parse file path i had this error :

“error”: “File does not exist. Check to make sure the file path to your csv is correct.”

Here is my code:

Parse.Cloud.define('trying', async req => {

  try {

    const query = new Parse.Query('File')

    const data = await query.get('4234ASxohU')
    const file = data.get('file')
    await file.getData()
    const csvFilePath = file._requestTask.path;
    //csvFilePath get me this : "/7VnTkgxVQQHBKmoPe6Ijn0kubLC9lZtc1tEcCw7C/987adf2e257e2d64104eac38829549eb_deneme.csv"
    const csvFile = await csv().fromFile(csvFilePath);
    return csvFile;
  } catch (error) {
    throw error;
  }
});

Your main problem is that data.get('file') won’t return a file path (e.g ./cloud/a.csv), but instead a PFFile referenced from your database / File Storage. fs can’t reference these locations as they aren’t stored in the NodeJS filesystem.

I’m not 100% sure, but you’ll probably need something like:

Parse.Cloud.define('trying', async req => {

  try {
    const query = new Parse.Query('File')
    const data = await query.get('4234ASxohU')
    const file = data.get('file')
    const csvString = await file.getData()
    const csvFile = await csv().fromString(csvString.toString());
    return csvFile;
  } catch (error) {
    throw error;
  }
});

Alternatively, you can write the CSV to ./cloud/controllers/a.csv using fs.writeFile

I solved my problem sending a request to url. Thanks.

Parse.Cloud.define('trying', async req => {
  try {
    const query = new Parse.Query('File')
    const data = await query.get('4234ASxohU')
    const fileUrl = data.get('file').url
    const csvFile = await csv({ delimiter }).fromStream(request.get(fileUrl))
  } catch (error) {
    throw error;
  }
});