admin管理员组文章数量:1278910
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.
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.
Share Improve this question edited Jun 28, 2019 at 10:22 Someone 3,5781 gold badge27 silver badges43 bronze badges asked Jun 28, 2019 at 10:19 nadav atarnadav atar 1751 gold badge4 silver badges9 bronze badges 1- The problem is on "url" argument not on "downloadUrl" parameter.. – Konstantinos Commented Jun 28, 2019 at 10:22
2 Answers
Reset to default 8You 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))
本文标签:
版权声明:本文标题:javascript - axios The "url" argument must be of type string. Received type undefined error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741224760a2361649.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论