recently I faced the problem that the result from database is not as not be able to display on the page. What am I doing wrong ?
This is the function code which is located in index.js
res.locals.get_user_name = function () {
User.findOne({_id: 2}).exec().then(function (user) {
return user.name;
});
};
And this is a code template EJS
<div class="name"><% get_user_name() %></div>
Related
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
});
});
I am learning Express with Mongoose and I am rendering a page where I run a forEach on a list of "campgrounds" from a MongoDB. My understanding is that when running the .find function it is optional to pass the err argument and run an if statement. But when I remove the err argument and the if statement altogether I get a "cannot run forEach on null while when I add it back (no other changes) my code runs smoothly. Not a problem when I add it back but I'm trying to understand what's going on in the background. Thanks in advance!
App.js code
//Create the camgrounds route
app.get("/campgrounds", function(req, res) {
//Get Campgrounds from DB
Campground.find({}, function(err, dbCampgrounds) {
//check for error
if (err) {
console.log(err);
//render the campgrounds page passing in the campground info from the db
} else {
res.render("campgrounds", {
renderCampGround: dbCampgrounds
});
}
});
});
And ejs file code
<div class="row">
<% renderCampGround.forEach(function(x){ %>
<div class="col-med-3 col-sm-6">
<div class="thumbnail">
<img src="<%= x.image %>">
</div>
<div class="caption">
<h4 <%= x.name %>>
</div>
</div>
</div>
<% }); %>
</div>
You are using the callback function so, all callbacks in the Mongoose use the pattern callback(err, data). So, if an error occurs while executing the query the error parameters will contain the error document and data will be null. If the query runs successfully then the error parameter will be null. But it is important to notice that not finding a document is not an error.
If you do not specify callback function then API will return the variable of type Query. Have a look.
So, if you don't want to use callback function then it will look like this.
//Create the camgrounds route
app.get("/campgrounds", function(req, res){
var campgrounds = Campgrounds.find({});
//execute a query at later time
campgrounds.exec(function (err, data) {
if (err) {
console.log(err)
}
else {
console.log(data)
}
}):
});
I am learning how to display mongodb data in html, but the code cannot work when I learn from network. I want to create the button to change the page to view data, and I don't know how to render data to ejs.
I try to find some method to solve the problem in network, but most of them are not the problem which I get.
code of find data
app.post('/viewdata', function (req, res) {
res.render('staffDisplay');
try{
MongoClient.connect(uri, function(err, client) {
if(err) {
console.log('Error occurred while connecting to MongoDB Atlas...\n',err);
}else{
var game=[];
var collection = client.db("GameDB").collection("Game");
var result = collection.find();
result.forEach(function(error,result){
if(error){
console.log(error);
}else{
if(result != null){
game.push(result);
}else{
console.log(game);
res.render('views/staffDisplay.ejs',{game:result})
}
}
})
console.log('show');
client.close();
}
});
}catch(ex){
throw new Error(ex.toString());
}
});
display.ejs
//skip the html code
<ul>
<% for(var i=0;i<=game.length;i++) {%>
<li><%=game[i].gName%></li>
<li><%=game[i].gDesc%></li>
<li><%=game[i].gDate%></li>
<%}%>
</ul>
the result is display 'game is not define', how can I do?
Can you try to remove this res.render('staffDisplay'); on the first part then replace res.render('views/staffDisplay.ejs',{game:result}) with this res.render('staffDisplay.ejs',{game:result})
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
})
})
})
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>
<% } %>