javascript - 无法理解如何使用 API,有什么建议吗?
问题描述
如下图所示: 站点:https://docs.thecatapi.com/
//Cat Command
if (message.content.startsWith(`${prefix}cat`)) {
message.channel.send("Looking for a kitty...").then (m => {
m.edit(`Found one!`)
})
fetch("https://api.thecatapi.com/v1/images/search")
.then(res => res.json()).then(body => {
if(!body) return message.reply(" whoops. I broke, try again!")
const catEmbed = new Discord.MessageEmbed()
.setTitle(" Meowww..")
.setColor(`#a87f32`)
.setURL(body.url)
.setImage(body.url)
message.channel.send(catEmbed)
})
}
我正在使用如上所示的这段代码,虽然它没有返回 url,但我做错了什么?
解决方案
可能问题出在sintax中。尝试使用它进行调试console.log()
以确保您正在获取服务器;
fetch("https://api.thecatapi.com/v1/images/search").then(res => res.json()).then(body => {
if(!body) return;
/* Check the response content */
console.log(body);
}).catch(err => {
/* Handle any possible error that may be occoring */
console.log(err);
});
现在您可以从浏览器控制台调试它。
推荐阅读
- haskell - 异步代码在 haskell 中的运行速度比同步版本慢
- azure-active-directory - Azure 广告 - Powershell - 如果用户是组 b 的成员,则从组 a 中删除用户
- libreoffice - 如何通过 Calc Macro 以编程方式打开和修改 ODT 文件
- java - 如何检查文件或控制台是否与标准输出相关联?
- postgresql - 每个函数/过程的 Postgres 日志目标
- python - Django 在注册表单中使用选择选项.py
- c - 循环缓冲区在第 6 个元素之后没有给出正确的缓冲区大小
- php - php 致命错误:require_once(): 无法打开所需的 'C:\xampp\htdocs\paribibi/public/index.php' (include_path='C:\xampp\php\PEAR')
- java - 我正在尝试在 Android Studio 中创建一个 Spinner
- ffmpeg - 调整覆盖视频的大小