1

I am using RXDB for the first time and i am a bit of trouble understanding how to add data to a document.

I have a "boards" collection, which i can get with db.boards;

Boards have "columns" and each column has a "tasks" array.

The relevant parts of the schema look like this:

export const boardSchema = {
    properties: {
        columns: {
            type: "array",
            items: {
                type: "object",
                properties: {
                    tasks: {
                        type: "array",
                        items: {},
                    },
                },
            },
        },
    },
};

I am trying to find a way to add a new task into a board > column > tasks array.

I have tried something like this:

const addTaskToColumn = async (boardId: string, columnId: string, task: Task) => {
    try {
        const boardsCollection = db.boards;
        const board = await boardsCollection.findOne(boardId).exec();
        const column = board.columns.filter((column: Column) => column.id === columnId)[0];

        return await column.update({
            $push: {
                tasks: task
            }
        })
    } catch (error) {
        console.error("There was an error deleting the column", error);
    }
};

But i get the error: there was an error deleting the column. TypeError: column.update is not a function. Why "deleting" the column?!

i have also tried:

return await column.patch({
     tasks: [...column.tasks, task]
})

but it also doesn't work. Error there was an error deleting the column. TypeError: column.patch is not a function.

Any help will be much appreciated...

1 Answer 1

0

I'm not sure if there is a better way to do this, but i ended up using this:

const addTaskToColumn = async (boardId: string, columnId: string, task: Task) => {
    try {
        const boardsCollection = flumenDB?.boards;
        if (!boardsCollection) {
            throw new Error("Could not get the boardsCollection!");
        }

        const board = await boardsCollection.findOne(boardId).exec();

        if (!board) {
            throw new Error("Could not get the board!");
        }

        const updatedColumns: Column[] = [];

        board.columns.forEach((column: Column) => {
            if (column.id === columnId) {
                const updatedColumn: Column = JSON.parse(JSON.stringify(column));
                updatedColumn.tasks.push(task);
                updatedColumns.push(updatedColumn);
            } else {
                updatedColumns.push(column);
            }
        });

        return board.update({
            $set: { columns: updatedColumns },
        });
    } catch (error) {
        console.error("There was an error deleting the column.", error);
    }
};

Not the answer you're looking for? Browse other questions tagged or ask your own question.