简体   繁体   中英

Discord.js snekfetch body undefined problem

    const Discord = require("discord.js")
    const snekfetch = require("snekfetch")
    const client = new Discord.Client({disableEveryone: false});
    //const CSGO = require("csgo-api"); // Import the npm package.
    //const jb = new CSGO.Server('185.198.75.5', '27015') // Set the IP with port.
    
    var prefix2 = "!"
    
    client.on('ready', async ()=> {
        snekfetch.get("http://query.li/api/csgo/185.198.75.5/27015").then(r => console.log(r.body.game.players.name));
        //jb.getOnlinePlayers().then(data => console.log(data)) // Get & log the data
   });

Hello friends, I'm trying to print the players section on http://query.li/api/csgo/185.198.75.5/27015 to message.channel.send but it gives undefined can you help me? I'm using Google Translate sorry my English so bad :/

You are trying to get the body of the result. But it is null. Your result contains these children: game, whois, status, banner_url, and cached.

And also, your players are an array. So you should select an index to console.log().

Try this:

    snekfetch.get("http://query.li/api/csgo/185.198.75.5/27015").then(r => console.log(r.game.players[0].name));

You can use this web site to beautify your response JSON.

在此处输入图片说明

EDIT:

If you want to print the names of all players then you can use a foreach() loop for players. Like this:

  snekfetch.get("http://query.li/api/csgo/185.198.75.5/27015").then(r => {
    r.game.players.forEach(player => {
      console.log(player.name)
    });
   }
  );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM