无法在节点js中读取未定义的属性“管道”

问题描述 投票:2回答:1

我正在尝试使用axiosfs下载图像当我使用节点app.js运行它时,它给出了一个错误,说管道无法定义

“TypeError:无法读取未定义的属性'pipe'”。

const axios = require('axios');
const fs = require('fs');
const Path = require('path');


async function download()
{
    const url ='https://www.google.co.in/imgres'
    const path = Path.resolve(__dirname,'files','image1.jpg')

    const response = axios
    (
        {
        method : 'GET',
        url:url,

        responseType:'stream'
        }
    )

    response.data.pipe(fs.createWriteStream(path))
        return new Promise((resolve,reject)=>{
            response.data.on('end',()=>{
            resolve()
        })

        response.data.on('error',err=>{
            reject(err);
        })
    }).catch();
}

download().then(()=>{
    console.log('download finished');
})
node.js npm axios
1个回答
0
投票

难道你不需要等待axios承诺完成吗?

Axios API

...
    const response = axios
    (
        {
        method : 'GET',
        url:url,

        responseType:'stream'
        }
    ).then(function(response) {

        response.data.pipe(fs.createWriteStream(path))
            return new Promise((resolve,reject)=>{
                response.data.on('end',()=>{
                resolve()
            })
...

根据脚本级别,你可以使用async / await来做到这一点我想,但我不是Axios专家。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.