Hello friends! I hope you are well.
So in this snippet of code, I have a list of ten servers I want to query. These ten servers are represented by the ten ports I have defined up top in the const "ports".
The idea is to use forEach() on "ports", and run the query on each server. An object is returned by each run of the query and added into the initially empty array "data".
THEN! After "data" has been loaded with the ten objects containing the status of my servers, I want it dished back to the client side!
However..... this is not what happens. Port.forEach() runs the query just fine, and adds into the array "data"... but not before res.json jumps the gun and sends off an empty data back to my soon to be disappointed client.
So far... I've tried callbacks and async/await... but I haven't figured out the syntax for it.. Any tips would be awesome! Thank you for your time friendos!
module.exports = app => {
app.get("/api/seMapServerStatus", (req, res) => {
const ports = [ "27111", "27112", "27117", "27118", "27119", "27110", "27115", "27116", "27113", "27114" ]
const data = []
function hitServers(port){
Gamedig.query({
type: "aGameType",
host: "theServer'sIP",
port: port
}).then((state) => {
data.push(state)
console.log("this is the server", state)
}).catch((error) => {
console.log("Server is offline");
});
};
ports.forEach(port => {
hitServers(port)
})
});
console.log("and here is the final server list", data)
res.json(data);
}