I have an interaction handler that shows a name of a user stored in mongodb one at a time from a click on a button component. There are two buttons, first user and second user, some sort of pagination. My problem is that the users array that received values from push on the first user button returns empty when called on the second user button.
The array does receive values just fine, but it really is empty when I check the value of users on console on the second button interaction (returns []).
So my question is how can I use the global array that received values from .push on my other interaction check conditions?
client.on("interactionCreate", async (interaction) => {
let page = 0;
let users = [];
if (interaction.customId === "firstUserBtn") {
db.collection("users")
.find()
.forEach((user) => users.push(user))
.then(() => {
const name = users[page].name;
console.log(name);
});
}
if (interaction.customId === "secondUserBtn") {
page += 1;
const name = users[page].name;
console.log(name);
}
});