2

I am developing an application in Express with Mongo. What I have to do is to read multiple records from db and send it to view. There I have to render those in table. This is, i have done so far:

my router:

router.route('/dashboard').get(function (req, res, next) {
    res.format({
        html: function () {
            mongoose.model('Register').find({'userid': req.session.email}, function (err, users) {
               var userMap={};
               users.forEach(function(user){
                  userMap[user._id]=user; 
               });
               res.json(userMap);
            });
        }
    });
});

Here is what I get in the view: enter image description here

I want to render it in the view like this:

table
  tbody
    tr
      td #{user.pid}
      td #{user.sid}
      td #{user.rfname}
      td #{user.des}

I refered to this but it doesn't tell how to access each record in the view? any help?

Community
  • 1
  • 1
Saani
  • 791
  • 7
  • 28
  • 68

3 Answers3

4

instead try like this so you will have all the data into the user object res.json({user:userMap}); So that you can access it using the user object.So use the object to access elements like user[0].pid

router.route('/dashboard').get(function (req, res, next) {   
  mongoose.model('Register').find({'userid': req.session.email},function (err, users) { 
  var userMap={}; 
  users.forEach(function(user){ 
    userMap[user._id]=user; 
  });
  res.render('myviewname',{user:userMap});
  }); 
});
Ketha Kavya
  • 558
  • 6
  • 18
  • i tried it like you said with the same result... :( – Saani Oct 19 '15 at 09:41
  • can you post the complete json object view that you get? – Ketha Kavya Oct 19 '15 at 09:44
  • Also you are sending an array object to view . So use the object to access elements like user[0].pid,.. like looping. – Ketha Kavya Oct 19 '15 at 09:52
  • i have posted that in the question, problem is, it does what is in the image above, rather i want it to access in the view. – Saani Oct 19 '15 at 09:52
  • router.route('/dashboard').get(function (req, res, next) { mongoose.model('Register').find({'userid': req.session.email}, function (err, users) { var userMap={}; users.forEach(function(user){ userMap[user._id]=user; }); res.render('myviewname',{user:userMap}); }); }); – Ketha Kavya Oct 19 '15 at 10:06
  • I have done this before and accessed the data like this `#{user.pid}` but it returns nothing. – Saani Oct 19 '15 at 11:17
1

Replace res.send() with res.json() so you get json object into view.

Miha2255
  • 61
  • 5
0

If you are using express generator or similar dir structure, you can pass the data from controller to view as an object and use a for-loop to iterate through each record and print in views. Check below example to get better idea :

routes/users.js (controller file)

/* GET users listing. */
router.get('/', async (req, res) => {
   const userData = await UserModel.find({}); // I'm using mongoose schema and model
   res.render('user', {userData: userData}); 
});

views/user.jade (view file)

table(border='1')
    tbody
    tr
        th First Name
        th Last Name
        th Email
        th Phone
    for user in userData
        tr
            td #{user.firstName}
            td #{user.lastName}
            td #{user.email}
            td #{user.phone}

This is my data :

[{ "_id" : ObjectId("5f9665e318ec5a064a7ae4ad"), "firstName" : "Anil", "lastName" : "Raj", "email" : "anil@gmail.com", "phone" : "7861171771", "password" : "123456" },                                                                                                                                                   { "_id" : ObjectId("5f966eb518ec5a064a7ae4ae"), "firstName" : "Arun", "lastName" : "Raj", "email" : "arun@gmail.com", "phone" : "7861171773", "password" : "123456" }]
Anil Raj
  • 19
  • 4