gpt4 book ai didi

javascript - Axios 错误 : . .. .data.pipe 不是函数

转载 作者:行者123 更新时间:2023-12-04 15:08:49 26 4
gpt4 key购买 nike

所以我基本上是在尝试使用 axios 从 url 下载图像,但我得到了这个错误:TypeError: streamResponse.data.pipe is not a function我进行此图像下载的功能如下(请注意,这是在一个类中):

/**
* 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);
});

}
我假设流响应的适配器是 xhr 的。无论如何,我已经尝试了两个适配器,并且都给出了完全相同的错误。虽然这两个请求都发生了(我可以在 devtools 中看到它们)。
所以没有混淆,我在文件顶部导入了适配器: const httpAdapter = require('axios/lib/adapters/http'); const xhrAdapter = require('axios/lib/adapters/xhr');我在这里做错了什么?

最佳答案

这是您的代码的更简单版本。
代码:

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',

// that the point!!!
// change responseType to stream
// pipe only work with 'stream'
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);

关于javascript - Axios 错误 : . .. .data.pipe 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65602941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com