'Unhandled error event and cannot read properties of null nodemon app crashed
[nodemon] starting `node app.js`
Server started on port 3000
node:events:505
throw er; // Unhandled 'error' event
^
TypeError: Cannot read properties of null (reading 'items')
at C:\Users\Debosmita\Desktop\Web Dev\todolist-v2-starting-files\app.js:97:19
at C:\Users\Debosmita\Desktop\Web Dev\todolist-v2-starting-files\node_modules\mongoose\lib\model.js:5010:18
at processTicksAndRejections (node:internal/process/task_queues:78:11)
Emitted 'error' event on Function instance at:
at C:\Users\Debosmita\Desktop\Web Dev\todolist-v2-starting-files\node_modules\mongoose\lib\model.js:5012:15
at processTicksAndRejections (node:internal/process/task_queues:78:11)
[nodemon] app crashed - waiting for file changes before starting..
This is the app.js file
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
mongoose.connect("mongodb://localhost:27017/todolistDB", {
useNewUrlParser: 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 the + button to add a new item.",
});
const item3 = new Item({
name: "<-- Hit this to delete an item.",
});
const defaultItems = [item1, item2, item3];
const listSchema = {
name: String,
items: [itemsSchema],
};
const List = mongoose.model("List", listSchema);
app.get("/", function (req, res) {
Item.find({}, function (err, foundItems) {
if (foundItems.length === 0) {
Item.insertMany(defaultItems, function (err) {
if (err) {
console.log(err);
} else {
console.log("Successfully saved default items to DB.");
}
});
res.redirect("/");
} else {
res.render("list", { listTitle: "Today", newListItems: foundItems });
}
});
});
app.get("/:customListName", function (req, res) {
const customListName = req.params.customListName;
List.findOne({ name: customListName }, function (err, foundList) {
if (!err) {
if (!foundList) {
//Create a new list
const list = new List({
name: customListName,
items: defaultItems
});
list.save();
res.redirect("/" + customListName);
} else {
//Show an existing list
res.render("list", {
listTitle: foundList.name,
newListItems: foundList.items,
});
}
}
});
});
app.post("/", function (req, res) {
const itemName = req.body.newItem;
const listName = req.body.list;
const item = new Item({
name: itemName,
});
if (listName === "Today") {
item.save();
res.redirect("/");
} else {
List.findOne({ name: listName }, function (err, foundList) {
if (!err) {
foundList.items.push(item);
foundList.save();
res.redirect("/" + listName);
} else {
console.log(err);
}
});
}
});
app.post("/delete", function (req, res) {
let checkedItemId = req.body.checkbox;
if (!mongoose.Types.ObjectId.isValid(checkedItemId)) {
checkedItemId = checkedItemId.replace(/\s/g, "");
}
Item.findByIdAndRemove(checkedItemId, function (err) {
if (!err) {
console.log("Successfully deleted checked item.");
res.redirect("/");
}
});
});
app.get("/about", function (req, res) {
res.render("about");
});
app.listen(3000, function () {
console.log("Server started on port 3000");
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
