首页 > 解决方案 > 无法理解如何使用 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,但我做错了什么?

标签: javascriptapirest

解决方案


可能问题出在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);
});

现在您可以从浏览器控制台调试它。


推荐阅读