[英]How to fix: “undefined response from fetch” Javascript
Been trying to fetch a link/image from an open API from the following guide: https://discordjs.guide/additional-info/rest-api.html#using-node-fetch but it is not working. 一直在尝试从以下指南中从开放的API中获取链接/图像: https : //discordjs.guide/additional-info/rest-api.html#using-node-fetch,但它不起作用。 I keep getting an undefined response.
我不断收到不确定的回复。
Already tried making async functions and so on but not getting any closer. 已经尝试制作异步函数等等,但是没有更进一步。 Also surrounded it by try-catch clausule to debug but not finding the answer.
还用try-catch子句将其包围,以进行调试,但找不到答案。
module.exports = {
name: 'poes',
description: 'Laat een random poes foto zien',
async execute(message, args) {
const fetch = require('node-fetch');
const {body} = await fetch('https://aws.random.cat/meow').then(response => response.json());
message.channel.send(body.file);
},
};
And this is where it is used: 这是它的使用位置:
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
}
);
Expected result following the Guide should be a random cat image. 遵循《指南》的预期结果应该是随机的猫图像。
The documentation you're using is incorrect in a couple of ways: 您使用的文档在两种方面是错误的:
const {body} = await fetch('https://aws.random.cat/meow').then(response => response.json())
That line assumes fetch
doesn't fail (with a 404, for instance). 该行假定
fetch
不会失败(例如,使用404)。 This is such a common mistake that I've written it up on my anemic little blog . 这是一个普遍的错误,我曾在贫乏的小博客上写下它 。
fetch
's promise only rejects on network errors , not HTTP errors. fetch
的promise只拒绝网络错误 ,而不是HTTP错误。 You have to check response.ok
or response.status
. 您必须检查
response.ok
或response.status
。
That the parsed result will have a body
property. 解析的结果将具有
body
属性。
It uses then
in an async
function, which makes very little sense. then
,它在async
函数中使用,这几乎没有意义。
But if I go to https://aws.random.cat/meow
, I get this JSON: 但是,如果我转到
https://aws.random.cat/meow
,则会得到以下JSON:
{"file":"https:\/\/purr.objects-us-east-1.dream.io\/i\/img_20131111_094048.jpg"}
There's no body
there, which is why you get undefined
for it. 那里没有
body
,这就是为什么您对此undefined
的原因。
Here's an example fixing all three issues: 这是修复所有三个问题的示例:
const response = await fetch('https://aws.random.cat/meow');
if (!response.ok) {
throw new Error("HTTP status " + response.status);
}
const body = await response.json();
// ^---^---- no { and }, we don't want to destructure
The response from the api is api的响应是
{
"file": "https://purr.objects-us-east-1.dream.io/i/r958B.jpg"
}
And you are saying it is 你说的是
{
"body" : {
"file" : ""
}
}
So you need to dump the brackets 所以你需要抛弃括号
const body = await fetch('https://aws.random.cat/meow')
.then(response => response.json());
or you need to look for file instead 或者您需要查找文件
const { file } = await fetch('https://aws.random.cat/meow')
.then(response => response.json());
console.log(file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.