mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
refactoring, fix todo removal from lists
This commit is contained in:
@@ -14,14 +14,22 @@ const TodoSchema = Schema({
|
||||
|
||||
TodoSchema.pre('save', async function () {
|
||||
const list = await TodoList.findById(this.list);
|
||||
list.todos.push(this.id);
|
||||
list.todos.push(this._id);
|
||||
await list.save();
|
||||
});
|
||||
|
||||
TodoSchema.pre('remove', async function () {
|
||||
const list = await TodoList.findById(this.list);
|
||||
list.todos.remove(this.id);
|
||||
list.todos.splice(list.todos.indexOf(this._id), 1);
|
||||
await list.save();
|
||||
});
|
||||
|
||||
TodoSchema.methods.toJson = function () {
|
||||
return {
|
||||
id: this._id,
|
||||
text: this.text,
|
||||
list: this.list,
|
||||
};
|
||||
};
|
||||
|
||||
mongoose.model('Todo', TodoSchema);
|
||||
|
||||
Reference in New Issue
Block a user