【问题标题】:Cannot read property 'pipe' of undefined in node js无法读取节点 js 中未定义的属性“管道”
【发布时间】:2018-08-30 11:52:41
【问题描述】:

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

“TypeError:无法读取未定义的属性‘管道’”。

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】:

    你不需要等待 axios promise 完成吗?

    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 专家。

    【讨论】:

      猜你喜欢
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2017-11-02
      • 2018-11-30
      • 2018-05-15
      • 2016-11-19
      • 1970-01-01
      相关资源
      最近更新 更多