I'm trying to make a journal but I'm stuck, I'm trying to make a for loop that posts the posts that I compose, in an array on the home page. I get connection between the app file and the ejs file because the inputs shows up when i log it in the terminal, I just can't get it to show up on the home page. I just want the title of the input to show up but i keep getting "SyntaxError: Unexpected token ')' in /user Desktop/frontEndNode-master/views/home.ejs while compiling ejs"..
<%- include("include/header"); -%>
<h1>Home</h1>
<P> <%= startingContent %> </P>
<%- console.log(posts);-%>
<%- include("include/footer"); -%>
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const { redirect } = require("express/lib/response");
const homeStartingContent =
"Lacus";
const aboutContent =
"Hac habitass";
const contactContent =
"Scelerisque";
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
const posts = [];
app.get("/", (req, res) => {
res.render("home",{ startingContent: homeStartingContent , posts:posts});
posts: [
{posts: posts},
{posts: posts}
]
//console.log(posts);
});
There's an extra ) somewhere.
To render HTML posts, you need to loop through array that you pass and extract values you need, where you also construct HTML.
Here's an example of looping through an object array with forEach and creating an <ul>
<ul>
<% posts.forEach(function(post, i) { %>
<li>Post <% i+1; %>. <%= post.name %> Date: <%= post.date %></li>
<% }); %>
</ul>