I'm so excited to post my first Stack Overflow query. :D
I recently started a Node.js course and with that, I just want to point out I'm not doing any hardcore stuff just yet.
I have a local node server running with nodemon. I started working with ejs and discovered that a forEach loop is melting my web page for some reason.
I tried doing research and sifting through my code removing certain parts piece by piece that weren't there before the issue until the issue went away and discovered that when I take the forEach loop out of my code in my ejs file, the problem goes away.
//this is my .js file located in my ./routes folder
const express = require('express');
const router = express.Router();
const admin_data = require('./admin');
router.get('/', (req, res, next) => {
const products = admin_data.products;
res.render('shop', {
page_title: 'Shop',
prods: products
});
});
//nothing too hectic.
//this is my ejs file located in my ./views folder.
<% if (prods.length > 0) { %>
<div class="grid">
<% forEach (var products in prods) { %> //if i remove this and it's closing '}' it works fine.
<article class="card product-item">
<header class="card__header">
<h1 class="product__title">product</h1>
</header>
<div class="card__image">
<img src="" alt="A Book">
</div>
<div class="card__content">
<h2 class="product__price">$19.99</h2>
<p class="product__description">A very interesting book about so many even more interesting things!
</p>
</div>
<div class="card__actions">
<button class="btn">Add to Cart</button>
</div>
</article>
<% } %>
</div>
<% } %>
I expect the code to render the page but instead receive an error:
"SyntaxError: Unexpected token { in C:\Users\Ruan
Buitendag\Documents\NodeJS\Practice Server\views\shop.ejs while
compiling ejs" in my vscode terminal and "Content Security Policy: The
page’s settings blocked the loading of a resource at inline
(“default-src”)" in my browser console.
EDIT: PS. I tried running the page in firefox developer edition, plain firefox and google chrome.
I think the problem is in your loop. Use forEach this way:
<% prods.forEach(function(product) { %>
<article class="card product-item">
<header class="card__header">
<h1 class="product__title">product</h1>
</header>
<div class="card__image">
<img src="" alt="A Book">
</div>
<div class="card__content">
<h2 class="product__price">$19.99</h2>
<p class="product__description">A very interesting book about so many even more interesting things!</p>
</div>
<div class="card__actions">
<button class="btn">Add to Cart</button>
</div>
</article>
<% }) %>
Alternatively you can use for...of loop like this:
<% for (var product of prods) { %>
...
Related
The error I am facing is as follows:
I have the following code written in server.js file. What am I doing wrong?
var express = require('express');
var app = express();
var instagram = require('instagram-node').instagram();
const ejsLint = require('ejs-lint');
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
instagram.use({
client_id:'1063599834134140',
client_secret:'66cbe60a931310beb7a8cf6d4ec7a12f '
});
app.get('/', function(req, res){
//
instagram.media_popular(function(err, medias, remaining, limit)
{
res.render('pages/index', {grams: medias});
});
});
app.listen(8080, function(err)
{
if(err)
{
console.log("Error Occured");
}
else
{
console.log("listening on port 8080");
}
});
And the following is index.ejs
<!DOCTYPE html>
<html>
<head>
<% include ../partials/head %>
<title></title>
</head>
<body class="container">
<header>
<% include ../partials/header %>
</header>
<main>
<div class="row">
<% grams.forEach(function(true){ %>
<div class="instagram-pic col-sm-3">
<a href="<%= gram.link %>" target="_blank">
<img src="<%= gram.images.standard_resolution.url %>" class="img-responsive">
</a>
</div>
<div class="instagram-bar">
<div class="likes">
<span class="glyphicon glyphicon-heart"></span>
<%= gram.likes.count %>
</div>
</div>
<div class="comments">
<span class="glyphicon glyphicon-comment"></span>
<%= gram.comments.count %>
</div>
<% }) %>
</div>
</main>
<footer>
<% include ../partials/footer %>
</footer>
</body>
</html>
Looks to me like your includes are wrong. Using the ejs-lint package that your error message references gives the following:
Unexpected token (4:17) in asdf.ejs
<% include ../partials/head %>
In looking at some ejs docs, the syntax they give for include is:
Includes are relative to the template with the include call. (This requires the 'filename' option.) For example if you have "./views/users.ejs" and "./views/user/show.ejs" you would use <%- include('user/show'); %>.
You'll likely want to use the raw output tag (<%-) with your include to avoid double-escaping the HTML output.
So in your case that would be
<%- include('../partials/head') %>
And of course, same for your other includes.
Also for future reference, it would be way better to copy/paste the error text into a code block in your question, instead of linking a screen shot.
I want someone to, please, help me to get over this hurdle. I'm trying to extract some documents that have the same properties from mongoDB. I'm using ejs template and mongoose.
Everything is working properly except that each of the documents appears more than once on the browser but I want each to appear once and in a sequential order.
I have the following code:
indexSchema:
const indexSchema = mongoose.Schema({
title: String,
subtitle: String,
quote: String,
image: String,
imageCaption: String,
author: String,
qualification: String,
body: String,
createdAt: {
type: Date,
default: Date.now
}
});
const Index = mongoose.model('Index', indexSchema)
module.exports = Index;
Index route:
app.get("/", async (req, res) => {
await Index.find((err, index) => {
if (err){
console.log("Sorry, posts not found successfully");
console.log(err);
} else{
console.log("Collections received successfully!")
res.render("index", {index:index});
index.ejs:
<%- include ('partials/header') %>
<div class="container">
<% index.forEach(index => { %>
<img src="<%= index.image %>" class="img-fluid mt-0.5" alt="Responsive image">
<div class="jumbotron jumbotron-fluid mt-1">
<div class="container">
<div class="row">
<div>
<h3><%= index.title %></h3>
<h5><%= index.subtitle %>.</h5><i><%= index.quote %></i>
</div>
</div>
</div>
</div>
<h3 class="mb-4">Featured Posts</h3>
View All Posts
<div class="card mt-4">
<div class="card-body">
<h4 class="card-title"><%= index.title %></h4>
<div class="card-subtitle text-muted mb-2">
<%= index.createdAt.toLocaleDateString(); %>
</div>
<div class="card-text mb-4">
<%= index.body %>
</div>
<div>
Read more<i class="fas fa-angle-double-right"></i>
</div>
</div>
</div>
<% }) %>
</div>
<%- include ('partials/footer') %>
From the index route, it's clear that I'm using the find() function. I know that this function brings back all the documents in the collection.
Since each document has a title, subtitle, author etc, it's difficult for me to make sure that each appears once on the browser using forEach loop.
The only means I know that would have rectified the issue for me is by targeting the id since each document has a unique id. But it seems I'm messing up everything because I do not know how to get this work in my HTML template since findById() appears to be incompatible with forEach(). Removing
forEach() from ejs also prevents all the images in the database from appearing on the browser.
I found that by using mongoose nexted schema I was able to achieve the same result I wanted. Though this is definitely not the best approach, I was able to use it to move to the next level until I gain more experience with mongoose and ejs
I'm Learning ReactJS Components and here my code is... I'm not getting where to save this file or in which folder I should keep this file?
<body>
<div id="anmol"></div> <script
src="https://unpkg.com/react#15/dist/react.min.js"></script> <script
src="https://unpkg.com/react-dom#15/dist/react-dom.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
<script type="text/babel"> var NavBar = React.createClass({
render: function() {
return (
<div className='navbar navbar-deafult'>
<div className='navbar-header'>
<a className='navbar-brand'>React</a>
</div>
<div className="collapse navbar-collapse">
<ul className="navbar-nav nav navbar-right">
<li> Home Page </li>
<li> About Us </li>
</ul>
</div>
</div>
); } }); var HelloWorld = React.createClass({
render:function() {
return(
<div> <h1>{this.props.children} </h1></div>
); } }); var destination = document.querySelector("anmol"); ReactDOM.render( <div> <NavBar/> <HelloWorld>
HelloWorld</HelloWorld</div>,destination); </script>
</body>
it look's like you are learning react, I strongly advice you to use https://github.com/facebook/create-react-app which will set-up whole environment with babel webpack and everything for you, so you can start to play with React. Greeting and have fun with React.
You can use codesandbox.io as well!
I created the example you were working on in there with updated syntax.
https://codesandbox.io/s/n9227v5xkp
I am trying to loop through my portfolio sections while inputting it with ejs. But it is throwing a 500 error. I created the loop to code less. The other pages work fine, but the title can't be found or the path is lost. It worked before I added the loop.
const express = require('express');
const router = express.Router();
/* GET portfolio page. */
router.get('/portfolio', (req, res, next) => {
let recentProjects = [{
title: 'ASL Logo',
paragraph: 'Re-branding of ASL Water Solutions'
},
{
title: 'Siva Creative Business Card',
paragraph: 'Re-branding of Siva Creative'
},
{
title: 'Encore Renovations',
paragraph: 'Website design for Encore'
}
]
res.render('portfolio', {
projects: recentProjects
});
});
module.exports = router;
<% include partials/header %>
<section class="content-section" id="portfolio">
<div class="container">
<div class="content-section-heading text-center">
<h3 class="text-secondary mb-0">
<%= title %>
</h3>
<h2 class="mb-5">Recent Projects</h2>
</div>
<div class="row no-gutters">
<% for(let i = 0; i < projects.length; i++) { %>
<div class="col-lg-6">
<a class="portfolio-item">
<span class="caption">
<span class="caption-content">
<h2><%= projects[i].title %></h2>
<p class="mb-0"><%= projects[i].paragraph %></p>
</span>
</span>
<img class="img-fluid" src="img/portfolio-1.jpg" alt="">
</a>
</div>
<% } %>
</div>
</div>
</section>
<% include partials/footer %>
In your view, the variable title is not defined, you should pass it to the view just as you do for the recentProjects object:
res.render('portfolio', {
projects: recentProjects,
title: 'portfolio'
});
im sure this is silly, but i cant get it to work for the life of me, i just want to do a simple for statement with an if inside on ejs, but it keeps returning a unexpected catch event in express.
to set up the scene, i've got a JSON thats setting up a a registration form in nodejs using ejs as my template renderer, lets say I have the following code:
var userData:{
"username":"User name",
"email":"johndoe#gmail.com",
"password":"Please enter Password",
"birthdate":"date"
}
and i have a ejs file that goes
<h1 id="page-title"><%= title %></h1>
<div id="list">
<form action="user/create/" method="post" accept-charset="utf-8">
<div class="item-new">
<% for (var key in userData){ %>
<% if(key != 'birthdate'){ %>
<input class="input" type="text" name="<%= key %>" placeholder="<%= userData[key] %>"/>
<% }else{ %>
//do something
<% } %>
</div>
i've tried to define a variable beforehand and check the value of the definitions
If the example you provided is indeed all of your code, you aren't closing all of your tags, specifically, you aren't closing your for loop, one of your divs, or your form. When I close all of these, the view will render for me without errors. This is the ejs that I used:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style-min.css' />
</head>
<body>
<h1 id="page-title"><%= title %></h1>
<div id="list">
<form action="user/create/" method="post" accept-charset="utf-8">
<div class="item-new">
<% for (var key in userData){ %>
<% if(key != 'birthdate'){ %>
<input class="input" type="text" name="<%= key %>" placeholder="<%= userData[key] %>"/>
<% } else{ %>
<% } %>
<% } %>
</div>
</form>
</div>
</body>
</html>
And I used this function to render it:
exports.test = function(req, res){
res.render('test', { title: 'nodejs + ejs for if express returns a unexpected catch', userData:{
"username":"User name",
"email":"johndoe#gmail.com",
"password":"Please enter Password",
"birthdate":"date"
}});
};
When I use the above I get this as a result: