简体   繁体   English

axios “url”参数必须是字符串类型。 收到类型未定义错误

[英]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.我正在开发一个电子应用程序,该应用程序试图从 unsplash API 下载照片并将其设置为墙纸。 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:当我调用 API 时,我得到 200 OK 状态并获取下载 URL,但是当我尝试使用 axios 流方法下载照片时,我收到以下错误:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. UnhandledPromiseRejectionWarning:TypeError [ERR_INVALID_ARG_TYPE]:“url”参数必须是字符串类型。 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.当我尝试 console.log 函数内的 downloadUrl 参数时,它会打印一个值。 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 :这意味着,您使用downloadUrl作为键,而不是url

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

You need to change it to url :您需要将其更改为url

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

A proper example of axios from the doc :文档axios的正确示例:

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:但是在不同的情况下, url 变量不是命名为 url 而是不同的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ytdl:“url”参数必须是字符串类型。 接收类型未定义 - ytdl: The "url" argument must be of type string. Received type undefined 错误:“路径”参数必须是字符串类型。 收到未定义。 firebase deploy --only 功能 - Error: The “path” argument must be of type string. Received undefined. firebase deploy --only functions 图片上传错误:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 接收类型未定义 - Image Upload Error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined 尝试使用gh-pages部署我的React应用程序,但收到此错误消息:“file”参数必须是string类型。收到的类型未定义 - Trying to deploy my React app with gh-pages but got this error message : The “file” argument must be of type string. Received type undefined 等待 dTypeError [ERR_INVALID_ARG_TYPE]:“id”参数必须是字符串类型。 收到未定义 - Waiting for the dTypeError [ERR_INVALID_ARG_TYPE]: The “id” argument must be of type string. Received undefined (hashlips_art_engine-1.1.2_patch_v5) “路径”参数必须是字符串类型。 收到未定义 - (hashlips_art_engine-1.1.2_patch_v5) The "path" argument must be of type string. Received undefined TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到未定义和代码:'ERR_INVALID_ARG_TYPE' - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined and code: 'ERR_INVALID_ARG_TYPE' 'TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 接收类型未定义' - 'TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined' Webpack 类型错误'TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 接收类型布尔值(真)' - Webpack type error 'TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type boolean (true)' 错误:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 尝试 node utils/nftport/uploadFiles.js 时收到 undefined - error:TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined when trying node utils/nftport/uploadFiles.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM