【发布时间】:2021-06-06 12:51:50
【问题描述】:
我开始使用Node JS 进行开发,我正在创建一个应用程序来实现以下目标:
下载包含在 API 中找到的 JSON 信息的 CSV。我有一个内部文件,其中包含 JSON 所在的 url,我需要从该 url 中提取信息并将其下载为 CSV。我正在使用json-2-csv、node-fetch 和fs 模块。
我的问题:我无法访问 JSON 中包含的信息来下载它
这是我的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();
【问题讨论】:
-
I cannot access the information contained in the JSON to download it是什么意思?responseJson未定义吗?拨打getJson时是否有错误? -
您看到了什么错误?提供的内容不足以调试问题?无法访问信息是什么意思?此外,
getJson变量是一个函数,因此请确保您也处理它(它是异步的,因此请确保您等待结果)/ -
@eol 我的问题是我不知道如何遵循,我只找到从其他文件转换内部 JSON 的信息,但我不知道如何从 api 中做到这一点
-
我明白了 - 检查我的答案。
标签: node.js typescript api json2csv