first off, I am writing in node.js using modules discord.js, request, and request-promise. I am having trouble sending a ton (about 70) different requests to a server. I keep getting the same result from the different requests. 'https://api.mojang.com/user/profiles/' + entry.uuid + '/names'
That is the request I am making, I have a forEach loop going through a list of urls like this. Here is my code for the requests:
function getNames() {
count = 0;
msg.channel.send("Started getting Names");
urls.forEach(function() {
request(urls[count], function(err, res, body) {
if (!err && res.statusCode == 200) {
const obj = JSON.parse(body);
names[count] = body[0].name;
msg.channel.send(urls[count] + ", Username " + count + ": " + obj[0].name);
} else {
msg.channel.send("Username Error: " + err);
}
});
count++;
});
while (true) {
if (names.length != urls.length) {
msg.channel.send("Finished getting Names");
console.log("UUIDs:" + urls.join(", "));
console.log("Names:" + names.join(", "));
break;
}
}
}
The msg.channel.send
commands I am using for debugging my code (discord.js if you are curious). Now, my problem is this: msg.channel.send(urls[count] + ", Username " + count + ": " + obj[0].name);
always gives me a new url but the same username as the first request ( obj[0].name
is always the same). So, say I have 3 different urls the first URLs username is Evan, second is Jack, third is John. When I run the code, my names array would be ["Evan", "Evan", "Evan"]
instead of ["Evan", "Jack", "John"]
. I think it is because my code is not waiting for the request result before moving on. I am sure there is probably many better ways of doing this, I would appreciate any tips.
Demo: https://repl.it/@kallefrombosnia/OldfashionedQuintessentialConditions
// request package is deprecated so I use this one
const fetch = require('node-fetch');
// random mojang acc's
const urls = [
'https://api.mojang.com/user/profiles/7125ba8b1c864508b92bb5c042ccfe2b/names',
'https://api.mojang.com/user/profiles/97f2453a361b44fa9312c496b67f24fc/names',
'https://api.mojang.com/user/profiles/ac224782efff4296b08cdbde8e47abdb/names',
];
getNames = () => {
let names = [];
let round = 0;
// parse trough urls
urls.forEach((url, index) => {
// fetch url info
fetch(url).then(res =>
// json convert
res.json())
.then(jsonBody => {
// iteration level
round++;
// add name to the array
names.push(jsonBody[0].name);
// print some info about user
console.log(urls[index] + ", Username " + index + ": " + jsonBody[0].name);
// if this is last round and number of names is same as urls number print all info about users
if (round === urls.length && names.length === urls.length) {
console.log("Finished getting Names");
console.log("UUIDs:" + urls.join(", "));
console.log("Names:" + names.join(", "));
}
})
})
}
getNames();
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.