简体   繁体   中英

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.

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'
  }
});

Both work for me:

 url = 'localhost:4000/getsomething'

    axios({
                        method: 'get',
                        url,
                        auth: {
                            username: 'Blue',
                            password: 'PowerRanger'
                       }
           }).then(function(response){//do stuff
               }).catch(err => console.log(err))

BUT in the different case the url variable is not named url but differently:

    customurl = 'localhost:4000/getsomething'
    axios({
                        method: 'get',
                        url: customurl,
                        auth: {
                            username: 'Blue',
                            password: 'PowerRanger'
                       }
           }).then(function(response){//do stuff
                }).catch(err => console.log(err))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM