Message from JavaScript discussions

September 2020

— So before you retrieve the data, you should map the role IDs, then filter unique, and then get all with`Promise.all()`

Message permanent page

— 

But I am certain, any proper db tooling should have an in operator.. Like SQL: select * from roles where id in [0,1,2,3,4]

— That's different use case.

— Why? you want to get all roles for a set of users

— What is the expected end result?

— I want to get userId and populate the other object with that.

— For each user.

— ..

— Router.get("/users", async (req, res) => {
let users = [];
try {
listAllUsers();
async function listAllUsers(nextPageToken) {
var listUsersResult = await admin.auth().listUsers(1000, nextPageToken);
if (!listUsersResult)
return res.status(404).json({ msg: "No user found!" });

listUsersResult.users.forEach(async (userRecord) => {
userRecord.toJSON();
let uid = userRecord.toJSON().uid;
let mUser = await User.findOne({ uid }).populate("role", ["title"]).lean();
mUser.photoUrl = userRecord.photoURL;
mUser.email = userRecord.email;
mUser.name = userRecord.displayName;
users.push(mUser);
//console.log(users)
})
setTimeout(() => res.status(200).json({ users }), 2000);
// if (listUsersResult.pageToken) listAllUsers(listUsersResult.pageToken);
}

}

Message permanent page

— If console.log out of forEach even then users array is empty.

— 1. use map then you don't need to work with intermediate variables and fill them. you can then assign directly.

2. I have no idea of mongodb, but this looks inefficient. not sure what .lean() does. maybe there is some optimization magic

Message permanent page

— And.. can't you populate the data directly with one query?