Message from JavaScript discussions

September 2020

— What is userRecord?

— 

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

let len = 0;
listUsersResult.users.forEach(async (userRecord) => {
userRecord.toJSON();
let uid = userRecord.toJSON().uid;
let mUser = await User.findOne({ uid })
.populate("role", ["title"])
.lean();
if (mUser) len++;
mUser.photoUrl = userRecord.photoURL;
mUser.email = userRecord.email;
mUser.name = userRecord.displayName;
users.push(mUser);
if (len === listUsersResult.users.length) {
return res.status(200).json({ users });
}
});
if (listUsersResult.pageToken) listAllUsers(listUsersResult.pageToken);
}
} catch (err) {
console.log(err.message);
return res.status(500).json({ msg: err.message });
}
});

— Coming from the firebase

— Why do you need to fetch mUser first?

— Why not just run an update query?

— User.findOneAndUpdate is a thing

— Because I need to populate it with the user role which is coming from mongodb.

— Https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate

— That's fine...?

— Hold on

— Im not to allowed to store user credentials in mongo expect uid

— That's fine