[英]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.