I have a list of tasks that includes both completed and incomplete items. Additionally, I have two buttons - one for deleting an item (which is functional) and the other for marking an item as done or not.
I am struggling with creating a function to move items from one array (A) to another array (B) once they are checked off.
Below is my schema and how I display the arrays:
var todoSchema = new mongoose.Schema({
name: String
});
var todoList = [];
var compList = [];
var Todo = mongoose.model("Todo", todoSchema);
app.get("/", function(req, res){
Todo.find({}, function(err, todoList){
if (err) {
console.log(err);
} else {
res.render("todo.ejs", {todoList: todoList, compList: compList});
}
})
});
Here is the code for adding a new item:
app.post("/newTodo", function(req, res){
console.log("item submitted");
var newItem = new Todo({
name: req.body.item
});
Todo.create(newItem, function(err, Todo){
if (err) {
console.log(err);
} else {
console.log("Inserted item: " + newItem);
}
})
res.redirect("/");
});
And here is the code for deleting an item:
app.get("/delete/:id", function(req, res){
Todo.findById(req.params.id, function(err, Todo){
if (err) {
console.log(err);
} else {
console.log(req.params.id);
Todo.remove(function(err, Todo){
console.log("Deleted item");
res.redirect("/");
})
}
});
});