I’m a newbie in mongodb and I’m trying to create a kanban task management, so when the user logs in he can fetch all its boards and populate it each board with its corresponding data. Here are my schemas :
´´´
const BoardSchema = Schema({
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
boardName: {
type: String,
required: [true, 'Board name is required.'],
},
columns: [
{
type: Schema.Types.ObjectId,
ref: 'Column',
required: true,
},
],
})
const ColumnSchema = Schema({
columnName: {
type: String,
required: [true, 'Column name is required.'],
},
tasks: [
{
type: Schema.Types.ObjectId,
ref: 'Task',
},
],
board: {
type: Schema.Types.ObjectId,
ref: 'Board',
required: true,
},
});
const TaskSchema = Schema({
title: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
status: {
type: String,
required: true,
},
subtasks: [
{
type: Schema.Types.ObjectId,
ref: 'Subtask',
},
],
column: {
type: Schema.Types.ObjectId,
ref: 'Column',
required: true,
},
});
const SubtaskSchema = Schema({
title: {
type: String,
required: [true, 'Subtask title is required.'],
},
isCompleted: {
type: Boolean,
required: true,
default: false,
},
task: {
type: Schema.Types.ObjectId,
ref: 'Task',
required: true,
},
});
´´´
And this is what I’m trying to execute but it brings me an empty array of columns: //! Get boards by user
´´´
const getUserBoards = async (req = request, res = response) => {
const userId = req.user._id;
try {
const userBoards = await Board.find({ userId }).populate({
path: 'columns',
populate: {
path: 'tasks',
populate: {
path: 'subtasks',
},
},
});
res.json({
ok: true,
userBoards,
});
} catch (error) {
res.json({
ok: false,
msg: `Some error happened: ${error}`,
});
}
};
´´´
I’m doing it this way to handle easily the CRUD operations for each schema, but if anybody has a better idea to handle this please provide me some information.