Explaining a custom function:
const getNotificationsForLounge = async (lounge_id) => {
try {
const notifications = await Notification.aggregate([
{
$match: {
"lounge_join_request.lounge": lounge_id,
lounge_join_request: { $ne: null },
},
},
]);
console.log("🚀 ~ notifications:", notifications);
// Process notifications
return notifications;
} catch (error) {
console.log("🚀 ~ error:", error);
// Handle errors
}
};
The above function takes a lounge_id
and retrieves notifications where
notification.lounge_join_request.lounge
matches the given lounge_id
.
I also created another function that selects a random notification
, extracts its lounge_join_request.lounge_id
, and passes it to the first function.
const getNotificationWithRandomLoungeJoinRequest = async () => {
try {
const notification = await Notification.aggregate([
{
$match: {
category: "LOUNGE_JOIN_REQUEST",
lounge_join_request: { $ne: null },
},
},
{ $sample: { size: 1 } },
{
$lookup: {
from: "lounge_join_requests",
localField: "lounge_join_request",
foreignField: "_id",
as: "lounge_join_request",
},
},
{ $project: { _id: 0, lounge_id: "$lounge_join_request.lounge" } },
]);
const lounge_id = notification[0].lounge_id[0];
console.log(
"🚀 ~ file: loungeServices.js:663 ~ getNotificationWithRandomLoungeJoinRequest ~ lounge_id:",
lounge_id
);
// When passing this lounge_id to the previous function,
// It returns an empty array instead of expected results
return await getNotificationsForLounge(lounge_id);
} catch (error) {
console.log("🚀 ~ error:", error);
}
};
Even though I take lounge_id
from an existing notification
, the first function should at least display that specific notification. However, it always returns an empty array. Highlighted here:
const lounge_id = notification[0].lounge_id[0];
// Logs a lounge id
// 🚀 ~ file: loungeServices.js:663 ~ getNotificationWithRandomLoungeJoinRequest ~ lounge_id: 63ef344xxxxb4943355
// Which means there exists a notification
// Where notification.lounge_join_request.lounge equals this lounge_id
console.log(
"🚀 ~ file: loungeServices.js:663 ~ getNotificationWithRandomLoungeJoinRequest ~ lounge_id:",
lounge_id
);
// But, when I feed that lounge_id into this function
// Which searchs notifications where
// notification.lounge_join_request.lounge equals this lounge_id
// It logs an empty array
return await getNotificationsForLounge(lounge_id);
Any thoughts on why?
Models related to this scenario:
Notification.js
const NotificationSchema = new Schema({
lounge_join_request: {
type: Schema.Types.ObjectId,
ref: "lounge_join_requests",
default: null,
},
});
module.exports = Notification = mongoose.model(
"notification",
NotificationSchema
);
LoungeJoinRequest.js
const LoungeJoinRequestSchema = new Schema({
lounge: {
type: Schema.Types.ObjectId,
ref: "lounge",
required: true,
},
});
module.exports = LoungeJoinRequest = mongoose.model(
"lounge_join_requests",
LoungeJoinRequestSchema
);
Lounge.js
const LoungeSchema = new Schema(
{
name: {
type: String,
},
}
);
module.exports = Channel = mongoose.model("lounge", LoungeSchema);