I have two tables as permissions and roles. Roles have permission as id reference. When I am populating roles I am getting permissions for names.
So, now I am creating a user with the reference id of Role. When I am populating the data of users I am getting the role with permissions id only. I want to show the permissions name also when I am populating the data of the user with the role.
Role Schema
const roleSchema = mongoose.Schema({
name: { type: String, required: true },
permissionsId: [{ type: mongoose.Types.ObjectId, ref: "Permission" }]
});
Role output
{
"roles": [
{
"_id": "629bc223d571677d5e898d50",
"name": "Admin",
"permissionsId": [
{
"name": "categoryList"
},
{
"name": "someup"
}
]
}
],
"message": "Success"
}
user schema
const userSchema = mongoose.Schema({
name: { type: String, required: true },
img: String,
username: String,
email: { type: String, required: true },
password: { type: String, required: true },
mobile: String,
address: String,
role: { type: mongoose.Types.ObjectId, ref: "Role" },
status: { type: String, enum: ['active', 'inactive'] },
verified: { type: Boolean, default: false },
updatedAt: { type: Date, default: Date.now },
createAt: { type: Date, default: Date.now }
});
User output
{
"users": [
{
"_id": "629f273d84902d0201aec30e",
"name": "Soumik",
"img": "https://i.ibb.co/0QTSPMj/p-4.jpg",
"username": "soumik9",
"email": "soumik@gmail.com",
"password": "$2b$10$V9VxSq5bdUGAZFV/04s/7.A62gP4shEC0/rnQel9VmvrAX/lFrqv.",
"role": {
"_id": "629bc223d571677d5e898d50",
"name": "Admin",
"permissionsId": [
"629bb7d271d26f033ac0bce8",
"629bbd9b62497eee720cc78e"
]
},
"status": "active",
"verified": false,
"updatedAt": "2022-06-07T10:23:57.633Z",
"createAt": "2022-06-07T10:23:57.633Z"
}
],
"message": "Success"
}
Expected user output
{
"users": [
{
"_id": "629f273d84902d0201aec30e",
"name": "Soumik",
"img": "https://i.ibb.co/0QTSPMj/p-4.jpg",
"username": "soumik9",
"email": "soumik@gmail.com",
"password": "$2b$10$V9VxSq5bdUGAZFV/04s/7.A62gP4shEC0/rnQel9VmvrAX/lFrqv.",
"role": {
"_id": "629bc223d571677d5e898d50",
"name": "Admin",
"permissionsId": [
{
"name": "categoryList"
},
{
"name": "someup"
}
]
},
"status": "active",
"verified": false,
"updatedAt": "2022-06-07T10:23:57.633Z",
"createAt": "2022-06-07T10:23:57.633Z"
}
],
"message": "Success"
}