Hello :). I am stuck in a node.js Blog project. I am using the templating engine edge for an online course that I am taking. The instructor has chosen to use edge as the templating engine. I am not sure if edge is the problem?
When I use:
res.render("index", {
posts,
});
in my code I get this error:
undefined:43
out += ` on ${this.context.escape(this.context.accessChild(this.context.resolve('post'), ['createdAt']).toDateString())}</p>\n`
^
TypeError: Cannot read properties of undefined (reading 'toDateString')
at eval (eval at run (/Volumes/Coding/NodeBlog/02-Class/node_modules/edge.js/src/Template/Runner.js:51:20), <anonymous>:43:121)
When I remove the posts from the code, it works but of course I have not access to the data.
res.render("index", {
});
Here is a full look at my code below:
<pre>const { response } = require("express");
const express = require("express");
const { request } = require("http");
const app = express();
const path = require("path");
const bodyParser = require("body-parser");
const { config, engine } = require("express-edge");
const mongoose = require("mongoose");
const Post = require("./database/models/Post");
const { error } = require("console");
const port = 4000;
mongoose.connect("mongodb://localhost:27017/nodeblog-dev01");
app.use(express.static("public"));
app.use(engine);
app.set("views", __dirname + "/views");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", async (req, res) => {
const posts = await Post.find({});
res.render("index", {
post,
});
});
app.get("/about", (req, res) => {
res.render("about");
});
app.get("/post", (req, res) => {
res.render("post");
});
app.get("/post/new", (req, res) => {
res.render("create");
});
app.post("/post/store", (req, res) => {
Post.create(req.body, (error, post) => {
res.redirect("/");
console.log(req.body);
});
});
app.get("/contact", (req, res) => {
res.render("contact");
});
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
What I have tried:
I thought it might have been a body parser problem, maybe I set it up wrong but it looks like it was done correctly