gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:56 31 4
gpt4 key购买 nike

我正在开发一个 Electron 应用程序,该应用程序尝试从 unsplash API 下载照片并将其设置为壁纸。当我调用 API 时,我得到 200 OK 状态并获取下载 URL,但是当我尝试使用 axios Stream 方法下载照片时,出现以下错误:

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

这是函数代码:

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...`);
}
});

当我尝试 console.log 函数内的 downloadUrl 参数时,它打印了一个值。我也这么做了

 console.log(typeoff(downloadUrl))

它打印了字符串。我希望你可以帮助我,提前致谢。

最佳答案

您正在使用解构:

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

这意味着,您正在使用 downloadUrl 作为 key ,而不是 url:

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

您需要将其更改为url:

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

来自 docaxios 的正确示例:

axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});

关于javascript - axios "url"参数必须是字符串类型。收到类型未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805135/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com