Saturday, April 13, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  173] [ 4]  / answers: 1 / hits: 9325  / 3 Years ago, wed, january 6, 2021, 12:00:00

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?


More From » node.js

 Answers
4

Got it working by sending the download request over to the main thread. Don't know why, but seems you can't access the axios data from a stream in the renderer.js file. Got it working now though after having a look here:


https://www.electronjs.org/docs/api/ipc-main


[#2008] Friday, January 1, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcos

Total Points: 331
Total Questions: 106
Total Answers: 104

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
marcos questions
Thu, Feb 24, 22, 00:00, 2 Years ago
Sat, Apr 17, 21, 00:00, 3 Years ago
Mon, Feb 1, 21, 00:00, 3 Years ago
Tue, Jan 26, 21, 00:00, 3 Years ago
Tue, May 26, 20, 00:00, 4 Years ago
;