Message from JavaScript discussions

September 2020

— ..

— 

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);
}

}

— 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?

— Like in bookshelf I can do users.fetchAll({ withRelated: ['role'] })

— Lean converts the mongo document to plain object.

— This is the only problem. I would've done it but users are not stored in mongodb. Im fetching them from firebase

Message permanent page

— Unfortunately not. I know this's is quite inefficient but im doing as "they" suggested

— Oh god.

— Ok this is unfortunate :D

— Yes