How to use packages in EJS template? - node.js

I'm trying to use timeago.js in an EJS template. I have tried to export the library like this:
src/lib/lib.js
const timeago = require('timeago.js');
exports.index = function(req, res){
res.render('links/list',{timeago: timeago});
}
The route is:
routes/links.js
router.get('/', (req, res)=>{
sequelize.query('SELECT * FROM links', {
type: sequelize.QueryTypes.SELECT
}).then((links)=>{
res.render('links/list', {links: links});
});
});
The EJS template is:
views/links/list.ejs
<div class="container p-4">
<div class="row">
<% for(i of links){ %>
<div class="col-md-3">
<div class="card text-center">
<div class="card-body">
<a target="_blank" href="<%= i.url %>">
<h3 class="card-title text-uppercase"><%= i.title %></h3>
</a>
<p class="m-2"><%= i.description %></p>
<h1><%= timeago.format(i.created_at); %></h1>
Delete Link
Edit
</div>
</div>
</div>
<% } %>
I need to use the library in the h1 to transform a timestamp I got from the database. However, I always get the same error: timeago is not defined.
How could I export Timeago correctly to use in EJS template? If I require the library in the routes file and send it to the EJS template through an object works perfectly, but not when I export it from another file.

I made the following test program to do a minimal test of timeago.js
const ejs = require('ejs');
const timeago = require('timeago.js');
let template = `
<% for(i of links){ %>
<h1> <%- i.created_at %>: <%- timeago.format(i.created_at) %> </h1>
<% } %>
`;
const renderData = {
links: [
{
created_at: new Date()
}
],
timeago
};
const output = ejs.render(template, renderData);
console.log(output);
Output:
<h1> Mon Sep 07 2020 00:01:57 GMT-0700 (Pacific Daylight Time): just now </h1>
So as long as you correctly pass the timeago object into your rendering data it will work.
The problem is likely here:
router.get('/', (req, res)=>{
sequelize.query('SELECT * FROM links', {
type: sequelize.QueryTypes.SELECT
}).then((links)=>{
res.render('links/list', {links: links});
});
});
Where you are not passing in the timeago object. This line:
res.render('links/list', {links: links});
Should probably be:
res.render('links/list', {links: links, timeago});
Edit:
More complete example using file paths specified in comments:
routes/links.js:
var express = require('express')
var router = express.Router();
const lib = require("../src/lib/lib");
router.get('/', (req, res)=>{
lib.index(req, res);
});
module.exports = router;
src/lib/lib.js
const timeago = require('timeago.js');
exports.index = function(req, res) {
const links = [
{
created_at: new Date()
}
];
res.render('links/list',{ timeago, links });
}

Related

node.js with express having trouble with using csurf

I am using ejs, and express to do this. I am not sure why this is keep giving me error of invalid token and I am using csurf dependency
router.get("/recipeUpload", function (req, res) {
const csrfToken = req.csrfToken();
// if (!res.locals.isAdmin) {
// return res.status(401).redirect("/");
// }
const step = req.session.steps;
console.log(csrfToken);
console.log(req.body._csrf);
res.render("uploadrecipe", { steps: step, csrfToken: csrfToken });
});
the code above is my get
<form action="/recipeUpload" method="POST" enctype="multipart/form-data">
<h2>
<%= csrfToken %>
</h2>
<% for (let i=0; i < steps ; i ++){ %>
<%- include("includes/step") %>
<% } %>
<label for="reference">Any referred resource?</label>
<input type="text" value="#" name="reference" id="reference">
<input type="hidden" name="_csrf" value="<%= csrfToken %>">
<button class="upload-item">Submit</button>
</form>
and this is my form
router.post("/recipeUpload", upload.array("image"), async function (req, res) {
console.log(req.body._csrf);
const uploadFiles = req.files;
const userData = req.body;
const reference = userData.reference;
const step = userData.step;
let imgPath = [];
for (let i = 0; i < uploadFiles.length; i++) {
imgPath.push(uploadFiles[i].path);
}
await db.getDB().collection("recipe").insertOne({
reference: reference,
step: step,
imgPath: imgPath,
});
res.redirect("/");
});
and this is my post and I checked with postman that csrfToken that is made in get is sent to ejs file and has value in input type hidden but when it is trying to submit it it gives ForbiddenError:Invalid csrf token. Can somebody share the knowledge why is this happening
I tried with csrf({cookie:true}) with cookie-parser but at that time it started to give me csrfmiscofiguration errors.

Page needs to be refreshed to load the data from an API in Node

So I'm building a simple web app that gets data from an API that needs to be displayed on the screen.
However after the Post request is made, the data from the API gets logged through the console.log() however it does not get displayed on the screen though I've got the correct code, it gets displayed after a manual refresh.
I'm using EJS to display the API data on the screen.
The app.js has the following:
var cityName='';
var temperature='';
var pressure='';
app.get('/',function(req,res){
res.render('home');
});
app.get('/results',function(req,res){
res.render('results',{
cityName: cityName,
pressure:pressure,
temperature:temperature,
}
);
});
app.post('/',function(req,res){
cityName=req.body.cityName;
const api_url= 'https://api.openweathermap.org/data/2.5/weather?q='+ cityName +'&appid=8bb235e1990db4b5ae16f92e920bad25';
https.get(api_url,function(output){
//console.log(output);
output.on('data', function(data){
const weatherData= JSON.parse(data);
// WEATHER
temperature=Number(weatherData.main.temp)-273.15;
pressure= Number(weatherData.main.pressure);
console.log('Temparature:'+temperature+' °C');
console.log('Pressure:'+pressure + ' hPa');
});
});
res.redirect('/results');
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function(){
console.log("Server ativated at port successfully");
});
The results.ejs file simply has the following:
<div class="container">
<div class="card-deck">
<div class="card bg-light mb-3" >
<div class="card-header">City</div>
<div class="card-body">
<ul>
<li><p class="card-text" name="cityName">City Name: <%= cityName %>.</p></li>
<li><p class="card-text" name="temperature">Temperature: <%= temperature %> °C.</p></li>
<li><p class="card-text" name="pressure">Pressure: <%= pressure %> hPa.</p></li>
</ul>
</div>
</div>
</div>
</div>
The home.ejs file has:
<div class="container1 container">
<form method="post" action="/">
<div class="brand-logo"></div>
<div class="brand-title">WEATHER MONITORING</div>
<div class="inputs">
<label>CITY NAME</label>
<input autocomplete="off" name="cityName" type="text" placeholder="Mumbai" />
<button type="submit">CHECK</button>
</form>
</div>
You redirect the user before the data loads
Move your redirect inside the callback
app.post("/", function (req, res) {
cityName = req.body.cityName;
const api_url =
"https://api.openweathermap.org/data/2.5/weather?q=" +
cityName +
"&appid=8bb235e1990db4b5ae16f92e920bad25";
https.get(api_url, function (output) {
//console.log(output);
output.on("data", function (data) {
const weatherData = JSON.parse(data);
// WEATHER
temperature = Number(weatherData.main.temp) - 273.15;
pressure = Number(weatherData.main.pressure);
console.log("Temparature:" + temperature + " °C");
console.log("Pressure:" + pressure + " hPa");
res.redirect("/results");
});
});
});

Object not found, using parse nodejs

I'm new using parse and I'm trying to get the objects from my database and displaying them with ejs using a for loop in my webpage. I'm using back4app as my database.
Here's what I'm doing:
const Car = Parse.Object.extend('Vehicle');
const query = new Parse.Query(Car);
app.get('/', function(req, res){
const VehicleInfo = [
{
VehicleName: query.get('Name'),
Description: query.get('Description'),
Price: query.get('Price'),
Rating: query.get('Rating'),
Route: query.get('Route'),
PassengerAmount: query.get('PassengerAmount')
}
]
try{
res.render('index', {
title: 'mainPage',
VehicleData: VehicleInfo
});
}catch(error){
throw error.message;
}
});
I query this and all 5 of my vehicles are displayed in the console.log but when trying to do the same in my .ejs file this shows up and only one div displays
enter image description here
Here's how I'm using the for loop
<% for (var CarInfo of VehicleData) { %>
<div class="row">
<div class="col-lg-4 col-md-6">
<!-- Car Item-->
<div class="rn-car-item">
<div class="rn-car-item-review">
<div class="fas fa-star"></div> <%= CarInfo.Rating %>
</div>
<div class="rn-car-item-thumb">
<a href="/car-single">
<img class="img-fluid" src="/images/car-1.jpg" alt="Black Sedan" srcset="/images/car-1.jpg 1x, /images/car-1#2x.jpg 2x"/>
</a>
</div>
<div class="rn-car-item-info">
<h3>
<%= CarInfo.VehicleName %>
</h3>
<p>Descripcion: <%= CarInfo.Description %></p>
<div class="rn-car-list-n-price">
<ul>
<li>Ruta: <%= CarInfo.Route %></li>
<li>Cantidad de Pasajeros: <%= CarInfo.PassengerAmount %></li>
</ul>
<div class="rn-car-price-wrap">
<a class="rn-car-price" href="/car-single">
<span class="rn-car-price-from">Desde</span>
<span class="rn-car-price-format">
<span class="rn-car-price-amount">$<%= CarInfo.Price %></span>
<span class="rn-car-price-per">/day</span>
</span>
</a>
</div>
</div>
</div>
</div>
<!-- End Car Item-->
</div>
</div>
<% } %>
I'm sure your code doesn't work like this, also not in the console. You need to run find or first in order to fetch objects.
The other problem is that your Promise hasn't been resolved and doesn't contain the result when you pass it on to the .ejs file. It works in the console because the result in the console will be updated once the Promise is resolved.
You need to do
const VehicleInfo = [];
const query = new Parse.Query(Car);
query.find().then(result => {
result.forEach(vehicle => {
VehicleInfo.push({
VehicleName: result.get('Name'),
Description: result.get('Description'),
Price: result.get('Price'),
Rating: result.get('Rating'),
Route: result.get('Route'),
PassengerAmount: query.get('PassengerAmount')
});
});
}).catch(error => {
console.error('error fetching objects', error);
});
Alternatively you can await the result for cleaner code:
app.get('/', async function(req, res) {
const VehicleInfo = [];
const query = new Parse.Query(Car);
try {
const result = await query.find();
result.forEach(vehicle => {
VehicleInfo.push({
VehicleName: result.get('Name'),
Description: result.get('Description'),
Price: result.get('Price'),
Rating: result.get('Rating'),
Route: result.get('Route'),
PassengerAmount: query.get('PassengerAmount')
});
});
} catch (error) {
console.error('error fetching objects', error);
}
});
Here's more about Promises in JavaScript

ForEach not working on mongodb but it work fine in locally array declaration

error of the code
My forEach loop is not working in ejs file while trying to fetch data from data base (mongodb) it contain a collection namely campground.but it work fine when I stored a collection of object in a array in myapp.js code.but when i m trying to implement through storing data in database it shows a error of campground.forEach is not a function
Here is my code:
myapp.js
var express=require("express");
var app=express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:true}));
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/campit");
var campgroundSchema=new mongoose.Schema({
name:String,
image:String
});
var campground=mongoose.model("campground",campgroundSchema);
app.get("/create",function(req,res){
campground.find({},function(err,campgrounnd){
if(err){
console.log(err);
}
else{
res.render("index.ejs",{campground:campground});
}
})
});
app.listen("3000",function(){
console.log("listening from server 3000");
});
index.ejs file which is suppose to render and it contain that for.Each loop
<div class="container">
<div class="row no-gutters">
<% campground.forEach(function(camp){ %>
<div class="col col-lg-4 img-thumbnail">
<img class="but" height="75%" width="100%" src="<%=camp.image%>" alt="image">
<div class="text-center">
<h6><%= camp.name %> </h6>
<button class="btn btn-outline-info " type="button" name="more info"> more info</button>
</div>
</div>
<% }); %>
</div>
</div>
any help will be highly appriciated..thank you
console.log the campground
The find method in MongooseJS returns a Query object (reference). There are a few ways you can do searches and return results.
campground.find().exec(function(err,result){
if(err){
console.log(err);
}
else{
res.render("index.ejs",{campground:result});
}
});
I think you are conflicting variable name
var campground=mongoose.model("campground",campgroundSchema); // here is a campground
app.get("/create",function(req,res){
campground.find({},function(err,result /* <-- */){ // the result is also campgrounnd
if(err){
console.log(err);
}
else{
res.render("index.ejs",{campground:result/* <-- */}); // pass result instead campgrounnd
}
})
});

Constructor & getter in Class of NodeJs is not working

Hi I have a problem at builder level and getter, I'm creating a small application of messages that are stored on the database. I used NodeJs for that, I created a class that allows to connect to the database and manage it,
The database contains a "message" table containing the string "id" "content" "creatd_d"
Here is the class code that I call message.js:
let connection = require("../config/connection")
let moment = require("moment")
class Message{
constructor (row) {
return this.row = row
}
get content(){
return this.row.content
}
get created_d(){
return moment(this.row.created_d)
}
static create(content, cb){
connection.query('INSERT INTO message SET content = ?, created_d = ?', [content, new Date()] , (err, results) => {
if (err) throw err
cb()
});
}
static all(cb){
connection.query('SELECT * FROM message order by created_d DESC', (err, rows) =>{
if(err) throw err
cb(rows.map((row) => new Message(row))) }) }
}
module.exports = Message
the goal of getter is to declare the module "moment" that allows to change the format of date, but the getter no longer works
Does anyone know, can this come from what please? thank you in advance
Remove the return in return this.row = row in the constructor. You are breaking the constructor and not returning the instance of Message.
For more information of my problem; this is the page "index.ejs":
<!DOCTYPE html>
<html>
<head>
<title>Ma premier app de NodeJs</title>
<link rel="stylesheet" type="text/css" href="/assets/Semantic/semantic.min.css">
</head>
<body>
<div class="ui main container">
<div class="ui fixed inverted menu">
Home
</div>
<br>
<br>
<h1>Bienvenue sur ma premier page ne NodeJs</h1>
<% if (locals.flash && locals.flash.error) { %>
<div class="ui negative message">
<%= flash.error %>
</div>
<% } %>
<% if (locals.flash && locals.flash.success) { %>
<div class="ui positive message">
<%= flash.success %>
</div>
<% } %>
<form action="/" method="post" class="ui form">
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
</div>
<button type="submit" class="ui red labeled submit icon button">
<i class="icon edit"></i> Send
</button>
</form>
<br>
<h3>Les Messages</h3>
<% for (message of messages){ %>
<div class="message-item">
<div class="ui message">
<%= message.content %>
<div class="ui date"><%= message.created_d %></div>
</div>
<br>
</div>
<%}%>
</div>
</body>
</html>
And this is the page serveur.js
let express = require("express")
let bodyParser = require("body-parser")
let session = require('express-session'); // Charge le middleware de session
let app = express()
//Moteur de template
app.set('view engine', 'ejs')
//Middleware
app.use('/assets', express.static("public"))
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(session({
secret: "monsession",
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}))
app.use(require('./middlewares/flash.js'))
// Les Routes
app.get('/', (req, res) =>{
let Message = require("./models/message")
Message.all(function(messages){
res.render('pages/index', {messages: messages})
})
})
app.post('/', (req, res)=>{
// test de disponibilité de message et si il est vide !!
if (req.body.message === undefined || req.body.message === '') {
req.flash('error', "Vous n'avez pas poster votre message")
res.redirect('/')
// res.render("pages/index", {error: "Vous n'avez pas entré aucun message"})
}else{
let Message = require("./models/message")
Message.create(req.body.message, function(){
req.flash('success', "Merci !")
res.redirect('/')
})
}
})
app.listen(8080)
thaks

Resources