Q:

message: 'Cast to ObjectId failed for value "xxxxxx " at path "_id" for model "item"', name: 'CastError', model: Model { item } }

I had to delete a todo from a page and I used findByIdAndRemove and also findByIdAndDelete but it always shows the above error I searched and used every method possible but they didnt sove my error please help me
// The code is as follows
//jshint esversion:6

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const date = require(__dirname + "/date.js");

const app = express();

app.set("view engine", "ejs");

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
app.use(express.static("public"));
mongoose.connect("mongodb://localhost:27017/todoDB", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useFindAndModify: false
});

const itemSchema = new mongoose. Schema(
  {
    name: String
  },
  // { _id: false }
);

const Item = mongoose.model("item", itemSchema);

const item1 = new Item({
  name: "Welcome to todolist"
});

const item2 = new Item({
  name: "Click + button to add your todo"
});

const item3 = new Item({
  name: "<-----Hit this to delete your items"
});

const defaultItems = [item1, item2, item3];

app.get("/", function(req, res) {
  const day = date.getDate();
  Item.find({}, function(err, foundItems) {
    if (foundItems.length === 0) {
      Item.insertMany(defaultItems, function(err) {
        if (err) {
          console.log(err);
        }
      });
      res.redirect("/");
    } else {
      res.render("list", { listTitle: day, newListItems: foundItems });
    }
  });
});

app.post("/", function(req, res) {
  const itemName = req.body.newItem;
  const item = new Item({
    name: itemName
  });
  item.save();
  res.redirect("/");
});

app.post("/delete", function(req, res) {
  const checkedItemId = req.body.checkBox;
  console.log(checkedItemId);
  Item.findByIdAndRemove(checkedItemId.str, function(err) {
    if (! err) {
      console.log("Successfully removed the checked item");
      res.redirect("/");
    } else {
      console.log(err);
    }
  });
});

app.listen(3000, function() {
  console.log("Server started on port 3000");
});

This was my code please help me I have been trying to solve this for the past three days and I am so close to breaking

A:

The string had a space at the end which i did not see so i used checkedItemId.trim() and it removed all the spaces and my code worked perfectly

