Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
97
rated 0 times [  101] [ 4]  / answers: 1 / hits: 6416  / 1 Year ago, mon, march 6, 2023, 12:00:00

I am currently learning Mongoose from Dr. Angela Yu's Course , however since mongoose has changed the syntax of find and other several functions, it is throwing above error


Here is the JavaScript Code


const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
var items = [];

app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
mongoose.connect("mongodb://localhost:27017/todoList", {useNewUrlParser: true});

const ItemSchema = new mongoose.Schema({
name: String
});

const Item = mongoose.model("Item", ItemSchema); // Items = collection name & ItemScema = Schema

// const <constantName> = new <ModelName> ({
// <fieldName> : <fieldData>
// })

//Some Default Items for the list
const Item1 = new Item({
name: "Welcome To Your To-Do-List!"
})

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];


// Item.find(AllItems){
// .then(function(){
// console.log("Here are the items");
// })
// .catch(function(err){
// console.log(err);
// })
// };





// Item.deleteMany( {name:"<-- Hit this to delete an item"})
// .then(function(){
// console.log("Deleted")
// })
// .catch(function(err){
// console.log(err);
// })



// Item.insertMany(defaultItems)
// .then(function () {
// console.log("Successfully saved defult items to DB");
// })
// .catch(function (err) {
// console.log(err);
// });
app.get("/", function (req, res) {

var today = new Date();

var options = { weekday: "long", day: "numeric", year: "numeric" , month: "numeric" };

var day = today.toLocaleDateString("en-GB", options);

Item.find({}, function(err, FoundItems){
console.log(FoundItems);
res.render("list", {kindOfDay: day, newItem: FoundItems});

})
});
app.post("/", function(req, res){
var item = req.body.newItem;

items.push(item);

res.redirect("/");

})
app.listen(3000, function () { //or process.env.PORT || 2000
console.log("Server started on port 3000");
})


The EJS code is below


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To Do List </title>
<link rel = "stylesheet" href = "css/styles.css">
</head>
<body>
<h1> <%= kindOfDay%></h1>
<ul>
<li>Buy Food</li>
<li>Cook Food</li>
<li>Eat Food</li>
<!-- <li><%= newItem %></li> -->
<%for (var i = 0; i < newItem.length; i++){ %>
<li><%= newItem[i] %></li>
<% } %>

</ul>

<form class="" action = "/" method = "post">
<input type = "text" name = "newItem">
<button type = "submit" name = "button">Add</button>
</form>
</body>
</html>

This is a to-do-List app and hence I need to render all the list items to my To-do-list , I have been trying to learn the alternatives but since I am a newbie I am unable to find a solution.


More From » mongoose

 Answers
37

This should work


async function getItems(){

const Items = await Item.find({});
return Items;

}

app.get("/", function (req, res) {

var today = new Date();

var options = { weekday: "long", day: "numeric", year: "numeric" ,
month: "numeric" };

var day = today.toLocaleDateString("en-GB", options);

getItems().then(function(FoundItems){

res.render("list", {kindOfDay: day, newItem:FoundItems});

});

});

[#1] Friday, December 30, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ericmaximilianz

Total Points: 252
Total Questions: 118
Total Answers: 87

Location: Virgin Islands (U.S.)
Member since Tue, Jul 7, 2020
4 Years ago
ericmaximilianz questions
Fri, Jan 14, 22, 00:00, 2 Years ago
Sat, Aug 21, 21, 00:00, 3 Years ago
Thu, Jan 28, 21, 00:00, 3 Years ago
;