T
T
TRUEC0DER2021-01-15 17:35:32
Node.js
TRUEC0DER, 2021-01-15 17:35:32

How to fix [object Promise]?

There is a "minecraft-server-util" module, and there is a method for getting information about the server, I added the ability to get online servers to the function, and everything seemed to work fine, but when I try to output the result somewhere other than the console, [object Promise] is displayed ].

async function getCurrentOnline(address, port) {
    try {
      const response = await util.status(address, { port: port, enableSRV: true, timeout: 5000, protocolVersion: 47 });
      return `${response.onlinePlayers}`;
    } catch (error) {
      throw error;
    }
}

getCurrentOnline('mc.hypixel.net.', 25565).then(count => count) // [object Promise]
getCurrentOnline('mc.hypixel.net.', 25565).then(count => console.log(count)) // Нормальный вывод

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@imegaded, 2021-01-15
@TRUEC0DER

I suspect that something like this =) The code is asynchronous and needs to be processed correctly.

const currentOnline = await getCurrentOnline('mc.hypixel.net.', 25565);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question