Problem with my ToDoList , findAndRemoveById dont work

25
November 26, 2020, at 05:40 AM

im doing my course and now making a ToDoList and upgrading it with mongoose but the problem is that in this video everything work fine but for me its showing ,,Argument passed in must be a single String ...,, when trying to use delete by checkbox enter code here

//jshint esversion:6
    enter code here
const express = require("express");
const bodyParser = require("body-parser");
const date = require(__dirname + "/date.js");
const mongoose = require("mongoose");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
//mongoose database
mongoose.connect("mongodb://localhost:27017/todolistDB", {useFindAndModify: false,useNewUrlParser: true,useUnifiedTopology:true});
const itemsSchema = {
  name: String
};
const Item = mongoose.model("Item" , itemsSchema);
const item1 = new Item({
  name: "Welcome to Your ToDoList!"
});
const item2 = new Item({
  name: "Hit + to add new item."
});
const item3 = new Item({
  name: "<- Hit this to delete item."
});
const defaultItems = [item1 , item2 , item3];

app.get("/", function(req, res) {
  Item.find(function(err , items){
    //add new items to our collection
    if(items.length === 0){
      Item.insertMany(defaultItems , function(err){
        if(err){
          console.log(err);
        }else{
          console.log("Added new items to DB.");
        }
      });
      res.redirect("/");
    }else{
      mongoose.connection.close();
   res.render("list", {listTitle: "Today", newListItems: items});
    }
  });
});
/////
/// Adding new item to our list
app.post("/", function(req, res){
  const itemName = req.body.newItem;
  const newItem = new Item({
    name: itemName
  });
newItem.save();
res.redirect("/");
});
//deleting item by checkbox
app.post("/delete" , function(req,res){
const checkedItemId = req.body.checkbox;
Item.findByIdAndRemove(checkedItemId, function(err){
if(err){
  console.log(err);
}else{
  console.log("Successfully deleted item.");
}
});
res.redirect("/");
});

/////
app.get("/work", function(req,res){
  res.render("list", {listTitle: "Work List", newListItems: workItems});
});
app.get("/about", function(req, res){
  res.render("about");
});
app.listen(3000, function() {
  console.log("Server started on port 3000");
});
and here where this checkbox is
  <div class="box">
  <%  newListItems.forEach(function(item){%>
    <form action="/delete" method="post">
    <div class="item">
    <input type="checkbox" name="checkbox" onChange="this.form.submit()">
    <p> <%= item.name %> </p>
    </div>
    </form>
  <%  }); %>
READ ALSO
How to migrate a whole React.js web-app to Typescript [closed]

How to migrate a whole React.js web-app to Typescript [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

37
How to handle errors in async Express?

How to handle errors in async Express?

Context: I have a rather large project which is a social media back endWe use MondoDB (w/ mongoose) and Express in JS

29
How can i redirect user to registration page when he failed to enter correct email (ID) in AMP form?

How can i redirect user to registration page when he failed to enter correct email (ID) in AMP form?

Please kindly advise if there is walk-around to redirect to new page when amp-form validation returns error? ie

43