Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

JavaScript
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.
JavaScript
res.render("index", {

});


Here is a full look at my code below:

JavaScript
 <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 expressEdge = require("express-edge");
const { config, engine } = require("express-edge");
const mongoose = require("mongoose");
// Pull in database model to get data from form to database
const Post = require("./database/models/Post");
const { error } = require("console");
const port = 4000;

// Mongodb Connection

mongoose.connect("mongodb://localhost:27017/nodeblog-dev01");

// Public Directory
app.use(express.static("public"));

// Edge Template Engine Configuraion
app.use(engine);
app.set("views", __dirname + "/views");

// BodyParser
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Routes
// Home Page
app.get("/", async (req, res) => {
  // Code below will find all of the post - await Post.find({})
  const posts = await Post.find({});

  // This will show you the post in the terminal
  // console.log(posts);

  res.render("index", {
    post,
  });
});
// --------------------------------------

// About Page
app.get("/about", (req, res) => {
  res.render("about");
});
// --------------------------------------

// Post Page
app.get("/post", (req, res) => {
  res.render("post");
});
// --------------------------------------

// Create a post route
app.get("/post/new", (req, res) => {
  res.render("create");
});

//  Store Post Data to data base
// After the user submit a new post he or she is redirected to the home page
app.post("/post/store", (req, res) => {
  // The object req.body will save the information coming from the browsers form on the
  //  new post create page
  // Create a new document for the database
  Post.create(req.body, (error, post) => {
    // Note to see the data the form must be - application/x-www-form-urlencoded
    res.redirect("/");
    console.log(req.body);
  });
});

// --------------------------------------

// Contact Page
app.get("/contact", (req, res) => {
  res.render("contact");
});
// ------------------------------------------

// // About Page Path
// app.get("/about", (request, response) => {
//   response.sendFile(path.resolve(__dirname, "pages/about.html"));
// });

// // Contact Page

// app.get("/contact", (request, response) => {
//   response.sendFile(path.resolve(__dirname, "pages/contact.html"));
// });

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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900