Saani
Saani

Reputation: 782

Express: How to send multiple records to the view and access it

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?

Upvotes: 2

Views: 1309

Answers (3)

Anil Raj
Anil Raj

Reputation: 19

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" : "[email protected]", "phone" : "7861171771", "password" : "123456" },                                                                                                                                                   { "_id" : ObjectId("5f966eb518ec5a064a7ae4ae"), "firstName" : "Arun", "lastName" : "Raj", "email" : "[email protected]", "phone" : "7861171773", "password" : "123456" }]

Upvotes: 0

Ketha Kavya
Ketha Kavya

Reputation: 588

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});
  }); 
});

Upvotes: 4

Miha2255
Miha2255

Reputation: 61

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

Upvotes: 1

Related Questions