How to display all users database (mongodb) details on .ejs file - node.js

I have a node.js website that i build with this tutorial:
https://scotch.io/tutorials/easy-node-authentication-setup-and-local
now i want in one of my views to print/display all users data.
in routes.js:
var User = require('../app/models/user'); //the connection to users database
app.get('/profile', isLoggedIn, function (req, res) {
res.render('profile.ejs', {
Users: User // get the user out of session and pass to template
});
});
and this is in my profile.ejs file:
<ul>
<% Users.forEach( function(err, user) { %>
<li><%= user %> </li>
<% }); %>
i get an error: "undefined is not a function" because of the <% Users.forEach( function(err, user) { %> line
what am i doing wrong?

You need to actually query the user collection via the model's find() query function which will return an array of user documents when executed, i.e.
var User = require('../app/models/user'); //the connection to users database
app.get('/profile', isLoggedIn, function (req, res) {
User.find({}).exec(function(err, users) {
if (err) throw err;
res.render('profile.ejs', { "users": users });
}
});
then render the list of user documents in your profile.ejs as:
<% users.forEach(function (user) { %>
<li>
<h1><%= user.name %></h1>
</li>
<% }) %>

Related

How to pass database values from node js to ejs file

Hi I am trying to add search bar in main page and search in the main page without changing to another page.
My user.js file
if (req.method == 'get'){
var username = req.query.username;
Users.find({username}, function (err, user) {
if(err) {
return res.render(template:'index')
}
res.render({template:'index', { user : user}});
})
}
my index.ejs file
<div class="search-user">
<% for user in user { %>
<div>
<p>Username : <%= user.username %></p>
<p>Age <%= user.age %></p>
<p>Status <%= user.status %></p>
</div>
<% } %>
</div>
This is giving me errors. May i know what did i do wrong?
Add a console message and check if you are getting data in user object
if (req.method == 'get'){
var username = req.query.username;
Users.find({username}, function (err, user) {
if(err) {
console.log('error', error)
return res.render('index')
}
console.log('user', user)
res.render('index', { user });
})
}
Further before iterating user of object just check something like this:
<div>
<p>Username : <%= JSON.stringfy(user) %></p>
</div>
By this you will get to know where error is being occured.
Refer to Res.render()
if (req.method == 'get'){
var username = req.query.username;
Users.find({username}, function (err, user) {
if(err) {
return res.render('index')
}
res.render('index', { user });
})
}

What is the proper way to check if id exists or not ? (ejs, mongoose)

If i have code like:
index.js
app.get('/link/:id', (req, res) => {
collection.find({ _id: req.params.id }, (err, collection) => {
if(err) res.send(err)
else {
res.render('link', { collection: collection });
}
link.ejs
<% if(collection != 'undefined') { %>
<li><%= collection.userName %></li>
<li><%= collection.address %></li>
<% }else{ %>
<p>Data not found</p>
<% } %>
But it also shows this error:
error:
Cannot read property 'userName' of undefined
so what is the solution?

holedata is not defined while loading ejs file after res.render

I am inserting the data into mongodb and then page rendering to success.ejs. In the success.ejs file I gave one link. When click link in the same success.js file data should fetch from mongodb and show in same success.ejs file. But before loading the success.ejs file after post the data in the console showing holedata is not defined error message.
My post data part in index.js file
app.post('/signingin', function(req, res) {
var item = {
name: req.body.name,
title: req.body.title,
age: req.body.age
};
mongo.connect(url, function(err, db){
assert.equal(null, err);
db.collection('satyamsoft').insertOne(item, function(err, result){
assert.equal(null, err);
console.log('item inserted');
db.close();
});
});
res.render('pages/success');
//console.log(req.body);
});
My success.ejs page
!DOCTYPE html>
<html lang="en">
<% include ../partials/head %>
<title>Post Jobs | Spyeer</title>
<body>
<% include ../partials/nav %>
<section class="get">
<h2>Data Inserted</h2>
Get Data
<div>
<ul>
<% holedata.forEach(function(data) { %>
<li><%= data.this.name %> - <%= data.this.title %> - <%= data.this.age %></li>
<% }); %>
</ul>
</div>
</section>
<% include ../partials/footer %>
<% include ../partials/scripts %>
</body>
</html>
get part code in index.js file
app.get('/getdata', function(req, res){
var resultArray = [];
mongo.connect(url, function(err, db){
assert.equal(null, err);
var cursor = db.collection('satyamsoft').find();
cursor.forEach(function(doc, err){
assert.equal(null, err);
resultArray.push(doc);
//console.log(resultArray);
//res.render('pages/getdata', {holedata: resultArray});
}, function(){
db.close();
res.render('pages/success', {holedata: resultArray});
});
Please help me advance thanks.
You just need to check every time page render there should be holedata either empty or with data like instead of res.render('pages/success'); you should write res.render('pages/success', {holedata: [] });

Error: can't set header after they are sent

I have two schemas, one for user and one for friendship system (follow system)
module.exports = mongoose.model('User', {
email: {
type: String,
unique: true,
lowercase: true
},
password:String,
profile: {
fullname: String,
gender: String,
role: {type: String, default: 'Autorizado'},
country: String },
});
and this for friend system
module.exports = mongoose.model('Friendship', {
follower: String,
followed: String
});
I'm using passport to auth my users... the problem is that I want lo load the user information and the friend information related to the user globally using res.locals I make this happen using 2 functions in login post route....
router.post('/login', userController.postLogin,friendController.getFollow);
this is my postlogin function
exports.postLogin = function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) return next(err);
if (!user) {
req.flash('errors', { msg: info.message });
return res.redirect('/login');
}
req.logIn(user, function(err) {
if (err) return next(err);
req.flash('success', { msg: 'Success! You are logged in.' });
res.redirect('/');
});
})
(req, res, next);
};
When I try to execute this code everything goes fine but when I log in I get the header error please how can I solved this or other way to get the user and follow information and use it in every view?.
EDIT 3: only works in the next two views
Single User page
<%= info.profile.fullname %>
<br>
<%= info.email %>
<br>
<%= info.profile.country %>
<br>
<%= user.follow %>
<br>
<% if(user && (user._id != info.id)){ %>
<br>
<form action="/follow/<%= info._id %>" method="post">
<button type="submit">Follow</button></form>
<%}%>
<br>
atras
List of Registered User
<%if (user) {%>
<h1>Hola <%= user.profile.fullname %> </h1>
<%}%>
<br>
<% users.forEach(function (user) {%>
<p><%= user.profile.fullname %></p>
<%});%>
Home
This is my index and in this view the user.follow doesn't work
<%if (!user) {%>
sign up
<br>
login
<%} else { %>
<h1>Hola <%= user.profile.fullname %> Eres lo Maximo</h1>
perfil
<br>
Logout
<br>
<%}%>
<br>
Lista de Usuarios
EDIT 4: new deserializeUser config.
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
FF.find({ follower:id }, function(err, follow) {
user.follow = follow;
});
done(err, user);
});
});
EDIT 5: This is my route code
router.get('/', function(req, res) {
res.render('index');
});
router.get('/login', userController.getLogin);
router.post('/login', userController.postLogin);
router.get('/logout', userController.logout);
router.get('/signup', userController.getSignup);
router.post('/signup', userController.postSignup);
router.get('/profile', passportConf.isAuthenticated, userController.getAccount);
router.post('/profile', passportConf.isAuthenticated, userController.postUpdateProfile);
You had friendController.getFollow which would've worked only for the request of '/login' page, so the follow object wouldn't have been persistently available on other routes. To fix that you had to put all that code inside the passport.deserializeUser function.
Inside the passport.deserializeUser function you could only call done once so you had to put the logic to find follow inside the callback of User.find so that you could attach follow to user.
This allowed you to remove the friendController.getFollow from your '/login' route which I think was the cause of your error.

how to send a flash message with res.redirect('/') sails js

How to send a flash message with res.redirect('/') in Sails?
When i check some condition in a controller then I want to redirect to another url, passing along a flash message.
I am new to Sails, so any help will be appreciated.
Controller action:
module.exports ={
index: function (req, res) {
if(req.param('key')){
req.flash('message', 'welcome key is present');
res.redirect('/view/');
} else {
req.flash('message', 'welcome key is not present');
res.redirect('/');
}
}
}
Thanks in advance.
Your code looks fine for the controller. In your view, you can access the flash message as req.flash('message'), so in an .ejs file for example it would be <%- req.flash('message') %>
What I find better for any redirect with data, is to set the http code to 307. It'll redirect with post/put/delete data.
req.flash('message');
res.redirect(307, '/');
An other solution would be to pass the message parameter in the route where you render the redirected template.
So let's take your example (slightly modified):
module.exports ={
index: function (req, res) {
if(req.param('key')){
req.flash('info', 'welcome key is present' );
res.redirect('/view/');
} else {
req.flash('info', 'welcome key is not present');
res.redirect('/');
}
}
}
Then in the route for 'view', you'll have something like this:
app.get('/view', function(req, res){
var messages = {};
if (typeof res.locals.messages.info != 'undefined'){
messages = res.locals.messages.info;
}
res.render('view.ejs', {
messages: messages
});
});
Then in your view.ejs:
<% if (typeof messages != 'undefined' && messages.length > 0) { %>
<% for (var i in messages) { %>
<div class="alert alert-info">
<%= messages[i] %>
</div>
<% } %>
<% } %>
Also in your main server.js file:
app.use(function(req, res, next) {
res.locals.messages = req.flash();
});

Resources