I have a simple nodejs program, where I am retrieving the data from mongodb using Mongoose and wanted to display it as a table using jade. Here is my code:
Server side code
router.get('/', function(req, res, next) {
//Query the user table
user.find(function(err, result){
res.render('index',{users: JSON.stringify(result)});
});
});
Jade code
div
form(action='/',method='post')
div(data-role='fieldcontain')
fieldset(data-role='controlgroup')
label(for='name') User Name
input(id='name',type='text',value='',placeholder='Type your username here',name='name')
div(data-role='fieldcontain')
fieldset(data-type='vertical', data-role='controlgroup')
label(for='fname') First Name
input(id='fname',type='text',value='',placeholder='Type your first name here',name='fname')
div(data-role='fieldcontain')
fieldset(data-type='vertical', data-role='controlgroup')
label(for='lname') Last Name
input(id='lname',type='text',value='',placeholder='Type your last name here',name='lname')
div(data-role='fieldcontain')
input(type='submit',value='Create Profile',data-transition='fade', data-theme='c')
- var userlist = {users}
div
table
thead
tr: th Users
tbody
each user in userlist
tr
td=user
And my model looks like this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var user = new Schema({
username: String,
firstname: String,
lastname: String
});
module.exports = mongoose.model('user', user);
The issue is that the message from the route to jade is not read by jade as JSON. I tried printing #{users} and it prints the result in full. However, in the each loop, its not able to separate out the two records.
I have a look at the questions:
Somehow, I feel that the message is not received as JSON and hence the issue. Please can you help?
I tried your code without JSON.Stringify its running fine .
Assuming you have record(s) for user in your database.
router.get('/', function(req, res, next) {
User.find(function(err,result){
console.log(result);
res.render('index', { title: 'Express' , users: result });
});
});
here is the code in index.jade
extends layout
block content
h1= title
p Welcome to #{title}
-var userlist = users
div
table.table.table-hover
thead
tr: th Users
tbody
each user in users
tr
td=user.firstname
td=user.lastname
td=user.username
when i started my server and hit the index page i got the following result.
But when i did the same thing with JSON.stringyfy() didn't get anything displayed here but data is there.
Hope it helps!! Good luck :)
If you want to create automatically your headers without specifying the fields:
Server side code
router.get('/', function (req, res, next) {
user
.find({})
.select('-_id -__v') // This removes hidden MongoDB fields
.lean() // This removes all mongoose fields and return a simple javascript object
.then(result => { // I'm using promises, but you can use callbacks as well
console.log(result);
res.render('index', { title: 'Express', users: result });
})
.catch(ex => console.log(ex));
});
Jade code
extends layout
block content
h1= title
p Welcome to #{title}
-var firstUser = users[0] || undefined;
if firstUser
div
h1 Users
table.table.table-hover
thead
tr
each value, key in firstUser
th=key
tbody
each user in users
tr
each value, key in user
td=value