如何在 nestjs 中从 URL 下载数据到 JSON 文件?

How to download data into JSON file from URL in nestjs?

我正在尝试从 nestjs 中的 URL 下载数据到 JSON 文件,但我不知道该怎么做。

下面是我的控制器函数参考这个

export class DataController {
constructor(private readonly httpService: HttpService) { }

@Get()
async download(@Res() res) {
    const writer = createWriteStream('user.json');

    const response = await this.httpService.axiosRef({
        url: 'https://api.github.com/users/prasadg',
        method: 'GET',
        responseType: 'stream',
        headers: {
            'Content-Type': 'application/json',
        }
    });

    response.data.pipe(writer); <--- Error here:  Property 'pipe' does not exist on type 'unknown'.

    return new Promise((resolve, reject) => {
        writer.on('finish', resolve);
        writer.on('error', reject);
    });
}

正如我上面提到的,它显示错误

error TS2339: Property 'pipe' does not exist on type 'unknown'

预期结果 : 我想将 json 数据写入服务器文件夹中的文件 (user.json)。

如何解决这个问题?

这是未经测试的,我不确定它是否有效。但是您的代码中存在一些误解,我希望可以澄清并为您指明正确的方向。

export class DataController {
constructor(private readonly httpService: HttpService) { }

@Get()
async download(@Res() res) {
    const writer = createWriteStream('user.json');

    // Here you await the result of the request
    // Meaning you wait here until all the data is recieved, which is then stored in response
    const response = await this.httpService.axiosRef({
        url: 'https://api.github.com/users/prasadg',
        method: 'GET',
        responseType: 'stream',
        headers: {
            'Content-Type': 'application/json',
        }
    });

    // Here you are trying to use a function called pipe on the property data on the response
    response.data.pipe(writer); <--- Error here:  Property 'pipe' does not exist on type 'unknown'.

    return new Promise((resolve, reject) => {
        writer.on('finish', resolve);
        writer.on('error', reject);
    });
}

我想你更愿意为数据管道做这样的事情

writer.pipe(response.data);

如果您使用的是 fs.createWriteStream,它可能看起来更像这样

writer.write(response.data);

您可能希望将请求放入流中,这样数据就可以在收到时流式传输,而不必等待它

export class DataController {
constructor(private readonly httpService: HttpService) { }

@Get()
async download(@Res() res) {
    const writer = createWriteStream('user.json');

    writer.pipe(this.httpService.axiosRef({
            url: 'https://api.github.com/users/prasadg',
            method: 'GET',
            responseType: 'stream',
            headers: {
                'Content-Type': 'application/json',
            }
        });
    );

    return new Promise((resolve, reject) => {
        writer.on('finish', resolve);
        writer.on('error', reject);
    });
}

就像我说的,代码没有经过测试,所以您必须使用它,但希望它能让您朝着正确的方向前进

这只是一个类型错误,请通过以下命令确保 axios 在您的项目中:

npm i axios 

然后只需在响应变量中声明类型

@Get()
    async download() {
        const writer = createWriteStream('user.json');
        // response variable has to be typed with AxiosResponse<T> 
        const response: AxiosResponse<any> = await this.httpService.axiosRef({
            url: 'https://api.github.com/users/prasadg',
            method: 'GET',
            responseType: 'stream',
        });

        response.data.pipe(writer);

        return new Promise((resolve, reject) => {
            writer.on('finish', resolve);
            writer.on('error', reject);
        });
    }