1

I am starting to develop with Node JS and I am creating an application to achieve the following:

Download a CSV with the JSON information found in an API. I have an internal file that contains the url where the JSON is located, I need to extract the information from that url and download it in CSV. I am using the json-2-csv, node-fetch and fs modules.

My problem: I cannot access the information contained in the JSON to download it

This is my controller:

import { Request, Response } from 'express';
const converter = require("json-2-csv");
const fetch = require("node-fetch");
const fs = require("fs");

class IndexController {
  public async index(req: Request, res: Response) {
  const api =req.query.api; //api1
  const url = conf.API_MOCS[`${api}`].url; //url containing the json
  const getJson = async () => {
        const response = await fetch(url);
        const responseJson = await response.json();
        return responseJson;
    };
  }
}

export const indexController = new IndexController(); 
sirtz
  • 75
  • 2
  • 9
  • 1
    What do you mean by `I cannot access the information contained in the JSON to download it`? Is `responseJson` undefined? Is there an error when you call `getJson`? – eol Jun 06 '21 at 13:06
  • 1
    what error do you see? the content provided is not sufficient to debug the issue? What do you mean by cannot access the information? Also, the `getJson` variable is a function, so make sure you handle it that war (it's async, so make sure you await the result) as well/ – Yash Kumar Verma Jun 06 '21 at 14:14
  • @eol My problem is that I do not know how to follow, I only find information to convert internal JSON from other files but I do not know how to do it from an api – sirtz Jun 07 '21 at 07:29
  • I see - check my answer. – eol Jun 07 '21 at 07:49

1 Answers1

0

The problem is that you never call your getJson function, i.e. a call like const jsonResponse = await getJson() is missing. It's not really necessary to do this in a separate function though. After getting the json, you need to pass it to the csv-converter and finally send the response through the res-object.

Here's a simplifed example (still needs error-handling) that fetches a json from a fake rest-api and converts it to csv and allows the user to download it:

const express = require("express");
const app = express();
const port = 3000;

const converter = require("json-2-csv");
const fetch = require("node-fetch");

app.get("/", async (req, res) => {
  const url = "https://jsonplaceholder.typicode.com/users";
  const response = await fetch(url);
  const jsonResponse = await response.json();

  const csvString = await converter.json2csvAsync(jsonResponse);
  res.setHeader("Content-disposition", "attachment; filename=data.csv");
  res.set("Content-Type", "text/csv");
  res.status(200).send(csvString);
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
eol
  • 23,236
  • 5
  • 46
  • 64
  • Ok I understand the problem now it works. And is it possible to download the CSV with a limit of lines and define some headers? – sirtz Jun 07 '21 at 15:02
  • Certainly possible, but a completely different question. Please post a new one with some code where you show what you've tried. Also, if this answer solved your issue, please accept it by clicking the checkmark on the left - thank you. – eol Jun 07 '21 at 15:11
  • Sorry I have been changing all the code to refactor it. What you put from the async it picks it up but I can't get it to work. I'm going to create another question, could you help me? I accept this – sirtz Jun 11 '21 at 11:29
  • My other question is: ``Download csv in Node JS with async / await``. Can you help me with that please, it would be of great help – sirtz Jun 11 '21 at 11:37