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

时间:2018-08-30 11:52:41

标签: node.js npm axios

我正在尝试使用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');
})

1 个答案:

答案 0 :(得分: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()
            })
...

根据脚本级别,我想也可以使用异步/等待来完成此操作,但是我不是Axios专家。