Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

javascript - Axios error: ... .data.pipe is not a function

So I am basically trying to use axios to download a image from a url, but I get this error:

TypeError: streamResponse.data.pipe is not a function

My function for doing this image download is below (note that this is inside a class):

/**
 * Download poster
 */
async downloadPoster() {

    // Writer stream where we want to download the poster image
    const writer = fs.createWriteStream(this.poster.file);

    // This grabs the second part of the image url that we want
    const resultsResponse = await axios({
        url: this.poster.url,
        method: 'GET',
        responseType: 'json',
        adapter: httpAdapter
    });

    // Zero results
    if (resultsResponse.data.total_results <= 0) {
        logger.log(language[Config.language].posterNotFound + this.movie.title, 'error');
        return false;
    }

    // Create the poster download URL
    var posterDownloadUrl = new URL(Config.api.posterUrl + resultsResponse.data.results[0].poster_path);

    const streamResponse = await axios({
        url: posterDownloadUrl,
        method: 'GET',
        responseType: 'stream',
        adapter: xhrAdapter
    });

    // Write data
    streamResponse.data.pipe(writer);

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

}

I assume that the adapter for a stream response is the xhr one. Anyways, I have tried both adapters and both gives the exact same error. Both requests does happen though (I can see them in the devtools).

And so there's no confusion, I have the adapters imported at the top of the file:

const httpAdapter = require('axios/lib/adapters/http');
const xhrAdapter = require('axios/lib/adapters/xhr');

What could I be doing wrong here?

question from:https://stackoverflow.com/questions/65602941/axios-error-data-pipe-is-not-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

here is a simpler version of your code.

Code:

const downloadImage = async (url) => {
    // Writer stream where we want to download the image
    const writer = fs.createWriteStream("./image.png");

    const streamResponse = await axios({
        url,
        method: 'GET',
        responseType: 'stream'
    });

    // Write data
    streamResponse.data.pipe(writer);

    writer.on('finish', () => console.log("Finished"));
    writer.on('error', () => console.error("Error while dowloading image"));
}
const imageLink = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png";
downloadImage(imageLink);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...