I'm encountering an issue where I have a list of IDs that I need to query Mongo with in a for loop, but it always returns an empty []. Initially, the queries were returning promises, so I switched from using forEach to a standard for loop as shown here and also tried using toArray(), both of which resulted in an empty [].
async function queryRoles(id) {
const db = await connectToMongo()
let response;
try {
response = await db.collection("permissions").find({"_id": xxx}).toArray();
console.log(await response);
} catch (error) {
console.log(error);
}
return response;
}
async function checkAuthorisation(list, groups) {
for (const item of list) {
const index = list.indexOf(item);
const rolesList = await queryRoles(item._id);
console.log(rolesList);
};
}
The rolesList is consistently empty and does not populate with any data.
Strangely, when I execute the same query for a single mongo document without using the for loop, I receive the expected response, confirming that the connection to MongoDB is functioning correctly.
What could be going wrong here? It's getting frustrating trying to figure this out!