Need help using EJS - node.js

I am exploring the use of the EJS templating system and am unsure of how to use it to get SQL data to be available to be rendered in a view.
In my app.js I have something like this:
conn.query("select name,age from people", function(err, people, moreResultSets) {
for (var i=0;i<people.length;i++){
console.log(people[i].NAME, "\t\t", people[i].AGE);
}
conn.close(function(){
console.log("Connection Closed");
});
});
And I have the following code to route the proper view:
app.get('/test1', function(req, res) {
res.render('pages/test1');
})
My confusion lies in making the people data available from the query
statement to be rendered in the view. All of the examples I have seen
have the variables defined locally inside the app.get code block and I
am unclear how to jump from that to my situation.
Thank you for any assistance you can provide!
-Andy

Render after you have the data.
app.get('/test1', function (req, res) {
conn.query("select name,age from people", function (err, people, moreResultSets) {
res.render('pages/test1', {
people: people
});
conn.close(function () {
console.log('Connection Closed');
});
});
});
HTML
<% if (people) { %>
<% for (var i = 0; i < people.length; i++) { %>
<div><%= people[i].name %></div>
<% } %>
<% } else { %>
<div>No people found</div>
<% } %>

Related

Display object from knex into ejs

I am trying to display a object in my ejs file but i'm not sure what i'm doing wrong, I keep getting Object, object. I can get it to display in the terminal but once I try and display it on the ejs page it no longer works.
all what i'm trying to do is count the number of rows and display that number on my ejs pages.
app.js
// render files
app.get('/', (req, res) => {
knex.where({ID: '1'})
.select()
.count()
.from("Table")
.then((results) =>{
res.render('Page', {Lcount: results});
})
});
I've tried to do is several ways on my ejs page but I can't seem to figure it out
ejs page
<%= Lcount %> //displays object, object
<%- Lcount %> //displays object, object
<% for (var i=0; i <Lcount.length; i++ ) { %>
<%- Lcount[i] %> // displays object, object
<% } %>
<% for (let i=0; i <Lcount.length; i++ ) { %>
<%= Lcount[i] %> //displays object object
<% } %>
for anyone having a similar issue I figured out what my the problem was with my code I needed to have a alias for my count so I can call it in my ejs. Since count() is one of the cases in knex when you are not returning a row of a table you are essentially making your own row based on your query.
app.get('/', (req, res) => {
knex("Table")
.where({ ID: '1' })
.count("ID" ,{as: 'count'}) //alias setup here
.first()
.then((results) => {
res.render('Page', {
title: 'Home',
Lcount: results
});
})
});
//ejs
<% = Lcount.count %>
Your knex query return an array of object:
[ { count: '11' } ]
As i understand you want to count the records by defined ID. So it would be more readable to write your query this way:
knex("Table")
.where({ ID: '1' })
.count()
.first() // Similar to select, but only retrieves & resolves with the first record from the query.
.then((results) => {
res.render('Page', { Lcount: results });
});
The result will be an object:
{ count: '2' }
Then you can if you want pass the result this way:
res.render('Page', {
Lcount: results.count
});
In your views/Page.ejs file:
<%= Lcount %>
In case you forgot to set the view engine:
app.set('view engine', 'ejs');
I don't understand how the result could be
{ ": 26 }
It seems to be malformed. To find where it goes wrong, what you can do, is to try this simple query directly from you database and paste the result:
SELECT count(ID) FROM Table WHERE ID = 1;

Show MongoDB Query Result On ejs page

I have a router which returns a specefic user's information based on the unique Object_id from MongoDB. This works fine, and I get the correct results, but they are returned on a "blank" page as a JSON object. I want to simply fetch the result and render them on my ejs page. Here are my route:
//Here are my router.js:
router.get('/user/get:id', function (req, res) {
MongoClient.connect(DBUri,{useUnifiedTopology: true }, function (err, db) {
let dbo = db.db(DBName);
const query = {_id: objectId(req.params.id)}
dbo.collection("Users").find(query).toArray(function(err, resultTasks) {
if (err) throw err;
res.send(resultTasks)
db.close();
});
});
});
//Here are my file.ejs:
<div class="card" v-for="post in filteredList">
<a v-bind:href="'/user/get' + post.id">{{ post.name }}</a>
</div>
Im still new so I know this is properly basic. I guess I have to change the res.send to something else, but now sure how.
You need to loop over resultTasks in your ejs template, something like:
<% resultTasks.forEach((post) => { %>
<div class="card" v-for="post in filteredList">
<a v-bind:href="/user/get/<%= post.id %>"><%= post.name %></a>
</div>
<%}); %>
Also, you probably need to change send in your endpoint with
dbo.collection("Users").find(query).toArray(function(err, resultTasks) {
if (err) throw err;
db.close();
res.render('<path of your ejs file>', {
resultTasks: resultTasks, // pass data from the server to the view
});
});

Pass result of Knex.js select to ejs template

I'm currently working on a project in express and I'm using knex.js for my queries and migrations.
I haven't touched node in a while so I'm a bit rusty. Essentially I'm trying to select records from one of my tables and then call the function in one of my routes to then iterate through and output it in my ejs template.
Unit Model
'use strict'
const knex = require('knex')(require('../../knexfile'))
module.exports = function(app) {
this.getAll = function() {
knex.select('id', 'unit_prefix', 'unit_name').from('units').then(function(units) {
return units;
})
}
return this
}
Then in my routes.js file:
app.get('/dashboard', ensureAuthenticated, function(req, res) {
// console.log(req.user)
console.log(unitModel.getAll)
res.render('dashboard', { user: req.user, units: unitModel.getAll })
})
If I console.log the result of unitModel.getAll I get [Function]. I've read about how knex uses promises and is asynchronous however I still haven't managed to use other answers to solve my rather simple issue.
In my dashboard.ejs file I have this code:
<h3>Jump to unit:</h3>
<%if (units.length > 0) { %>
<% units.forEach(function(unit) { %>
<div class="dashboard-course-item" tooltip="First year unit, covers the basics of web foundations">
(<%= unit.unit_prefix %>) <%= unit.unit_name %>
</div>
<% }) %>
<% } else { %>
<strong>Currently no units have been created.</strong>
<% } %>
I currently have one record in the units table and always see the Currently no units have been created. message.
What do I need to change to return an array or object that I can iterate over in my ejs template?
Thanks in advance!
Promises return their values asynchronously in their .then callback function, if you return the promise itself it will return undefined since in that moment the promise still hasn't resolved.
To make your code work you should do something like this:
Unit Model
'use strict'
const knex = require('knex')(require('../../knexfile'))
module.exports = function(app) {
this.getAll = function(){
return new Promise(function(resolve,reject) {
knex.select('id', 'unit_prefix', 'unit_name').from('units')
.then(function(units) { resolve(units);})
.catch(function(error) {reject(error);})
})
}
return this
}
routes.js
app.get('/dashboard', ensureAuthenticated, function(req, res) {
// console.log(req.user)
unitModel.getAll()
.then(function(units){
res.render('dashboard', {
user: req.user,
units: units
})
})
})

Trying to get data to display in a table

I'm working on my first node project using express and sequelize, and I'm not understanding how the page rendering works
I have the following function in my one of my models(sequelize):
getGreetings: function (req, res) {
Greeting.findAll({
}).then(function (data) {
console.log('Returned data for greetings: ' + data);
res.send({greetings:data});
})
}
Here is my route:
var Greeting = require('../models/greetings');
router.get('/', function(req, res) {
res.render('index', function(){
Greeting.getGreetings(req, res);
});
});
and my ejs table I want to display the data in:
<tbody>
<% for(var i=0; i < greetings.length; i++) { %>
<tr>
<td><%= greetings[i].name %></td>
<td><%= greetings[i].message %></td>
</tr>
<% } %>
</tbody>
This isn't displaying any of the html, but rather echoing out the json data. Can someone help explain why my html table isn't being populated?
but rather echoing out the json data.
This is because getGreetings() is always setting that as the response, by using res.send():
res.send({greetings:data});
To provide greetings to your view, you'll have to instead provide the data within the locals given to res.render():
res.render('index', { greetings: data });
The two methods don't cooperate with each other. Each is defined to end the response itself, so you'll only be able to use one per response.
If you revise getGreetings to return the promise created by .findAll():
getGreetings: function (req) {
return Greeting.findAll({
// ...
});
}
Then, the route handler can bind to it and decide how to make use of the result itself – whether it should use res.send() or res.render():
var Greeting = require('../models/greetings');
router.get('/', function(req, res) {
Greeting.getGreetings(req).then(function (greetings) {
res.render('index', { greetings: greetings });
});
});

Basic ExpressJS

I am a new bie of NodeJS. I am writing a simple application with a database integration.
For Rendering i used ejs template engine to render. upto now everything working fine. succeeded in few steps as follows,
DB Integration with mongoose.
Data retrieval by avoiding routes
Finally i want to display a list on users page which is coded like wise,
app.js
app.get('/users', function(req, res){
User.find({},function (err, users) {
console.log(users)
res.render('users', { title: 'Users', users : users });
});
console.log(users)
});
users.html
<ul>
<% for(var i=0; i<users.length; i++) { %>
<li><%=i%><%=JSON.stringify(users[i]) %></li>
<% } %>
</ul>
I need a an output something coded like this: but showing undefined.
<ul>
<% for(var i=0; i<users.length; i++) { %>
<li><%=i%><%=users[i].name %></li>
<% } %>
</ul>
Can anyone suggest what would be the mistake. i am unable to print the name.

Resources