Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios The "url" argument must be of type string. Received type undefined error

Im working on an electron app that is trying to download a photo from the unsplash API and set it as a wallpaper. When I call the API I get 200 OK status and get the download URL, but when I try to download the photo with the axios stream method I get the following error:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined

this is the function code:

ipcMain.on("getRandomWallpaper", async event => {
  const randomApi = `${apiGateway}/?client_id=${unsplashKey}`;
  const request = await axios({
    method: "get",
    url: randomApi
  });
  if (request.status === 200) {
    const downloadUrl = request.data.links.download;
    const imagePath = "./images";
    const download_image = async (downloadUrl, imagePath) => {
      await axios({
        downloadUrl,
        responseType: "stream"
      }).then(
        response =>
          new Promise((resolve, reject) => {
            response.data
              .pipe(fs.createWriteStream(imagePath))
              .on("finish", () => resolve())
              .on("error", e => reject(e));
          })
      );
    };
    download_image(downloadUrl, imagePath);
  } else {
    const status = request.status;
    console.error(`${status}: \n Something went wrong...`);
  }
});

When I tried to console.log the downloadUrl parameter inside the function it printed a value. Also I did

 console.log(typeoff(downloadUrl))

and it printed string. I hope you can help me, thanks in advance.

like image 628
nadav atar Avatar asked Sep 03 '25 01:09

nadav atar


1 Answers

You are using destructuring:

await axios({
    downloadUrl,
    responseType: "stream"
})

This means, You are using downloadUrl as key, instead of url:

await axios({
    downloadUrl: downloadUrl,
    responseType: "stream"
})

You need to change it to url:

await axios({
    url: downloadUrl,
    responseType: "stream"
})

A proper example of axios from the doc:

axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});
like image 186
Aritra Chakraborty Avatar answered Sep 04 '25 15:09

Aritra Chakraborty