Reputation: 74
I'm doing a to do list application. I use the following function to move a task in the to-do list to the done list. However, the "columnId" method I use as the url parameter and the "findById" method that I use return null. What do you think the problem might be?
public async Put(columnId: string, todo: ITodo): Promise<ITodo | null> {
try {
const editingTodo: ITodo | null = await Todo.findById(todo._id);
if (editingTodo !== null) {
const oldColumn: IColumn | null = await Column.findById(editingTodo.Column._id).exec();
if (oldColumn !== null) {
const todoIndexByOldColumn = oldColumn.Todos.indexOf(editingTodo._id);
oldColumn.Todos.splice(todoIndexByOldColumn, 1);
const newColumn: IColumn | null = await Column.findById(columnId).populate("Todos").exec();
console.log(newColumn);
if (newColumn !== null) {
newColumn.Todos.push(todo);
newColumn.save();
oldColumn.save();
editingTodo.save();
}
}
}
return editingTodo;
} catch (error) {
throw error;
}
}
Upvotes: 0
Views: 304
Reputation: 430
Here is crud operation for a todo list with mongoose.
// model todo
let todo = new schema({
description: { type: String },
heading: { type: String },
title: { type: String },
});
Below is the controller with logic for all operations.
// get All Todo List
let findAllTodoList = async (req, res, next ) => {
let foundAllTodo = await todo.findAll({});
res.send({ data: foundAllTodo });
};
// get Specific Todo
let findTodoById = async (req, res, next ) => {
let todo_id = req.params.todo_id;
let foundTodo = await todo.findById(todo_id);
res.send({ data: foundTodo });
};
// create Todo Element
let createTodo = async (req, res, next ) => {
let todo_obj = {
description: 'Here Add Element',
heading: 'Adding',
title: 'Add',
};
let foundTodo = await todo.create(todo_obj);
res.send({ data: 'success' });
};
// Destroy Todo Element
let DeleteTodoById = async (req, res, next ) => {
let todo_id = req.params.todo_id
let foundTodo = await todo.remove({ _id:todo_id });
res.send({ data: 'success' });
};
module.exports = {
findAllTodoList
findTodoById,
createTodo,
DeleteTodoById
};
Upvotes: 1