I have a MongoDb object with contains an array of objectIds
const User = mongoose.model(
"User",
new mongoose.Schema({
name: String,
email: String,
evals:[],
password: String,
position: String,
roles: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Role"
}
],
username: {
String
}
})
);
module.exports = User;
The Role is currently just a name and description:
const Role = mongoose.model(
"Role",
new mongoose.Schema({
name: String,
description: String
})
);
module.exports = Role;
I want to return the name of the role rather than the object ID by calling a function like this:
exports.getRoles = (req, res) => {
User.findOne({
$or: [
{ _userIds: req.params.userId }]
}).then((user) => {
const roleList = [];
for (let role of user.roles) {
Role.findOne({ "_id": role.toString() }).then((role) => {
console.log(role.name);
roleList.push(role.name)
}
)};
res.json(roleList);
})
}
As you can already see, this returns an empty array because it's not getting the results of the nested promises. I've gone around and around, and even written helper functions that achieve (unfortunately) the exact same results.
So, how am I supposed to get the results of the final array from the promises? I can see from the console.logs that the array is being filled, but the res.json(roleList) is occuring first.
Also, am I making this way too hard, and is there a simple way that MongoDb does this automagically? I could probably do this easily in SQL with a query, but that's not an option here.