desmond
desmond

Reputation: 65

using hidden fields in express js

Im trying to pass value of a hidden id field and use it to retriev records from mongodb and display it on profile page,after clicking "read more" on index page.This the index.ejs:

     <% for(i=0; i<users.length; i++){%>
        <div class="col-lg-3 center">
            <div class="text-center">
                <img class="img img-circle" src="<%= users[i].image %>" height="120px" width="120px" alt=""> <br>
                <h4><b><%= users[i].fname %> <%= users[i].lname %></b></h4>
                <ul class="list-inline social-buttons">
                <li><a href="<%= users[i].linkedin %>"><i class="fa fa-linkedin"></i></a></li>
                <li><a href="<%= users[i].gitHub %>"><i class="fa fa-github"></i></a></li>
                <li><a href="#"><i class="fa fa-twitter"></i></a></li>
                </ul>
                <input type="hidden" name="id" value="<%= users[i]._id %>" >
                <p><%=users[i].bio %>....<a href="prof">Read More</a></p>

            </div>
        </div><!-- col-lg-3 -->
        <% } %>

and here is profile.ejs:

 <div class="medium-4 small-12 columns">
    <h3> <%= users.fname %>  <%= users.lname %></h3>
    <p>Username: <%= users.username %></p>
    <p>Email: <%= users.email %></p>
    <p> Software Developer at <%= users.role %></p>
  </div>

and express routes ,users.js.

  app.get('/prof',function(req, res) {
     var id=req.body.id;
     var user = new User();
    mongoose.model('User').findById(id,function(err, users){
       console.log(users);
        res.render('pages/profile',{users:users});
     });
  });

This gives me an error "Cannot read property 'username' of null.."

What am I missing?

Upvotes: 4

Views: 3452

Answers (1)

chridam
chridam

Reputation: 103365

You are doing a GET operation yet you are trying to send the id value through the POST parameters which are grabbed using req.body.variable_name.

In this case you don't need a hidden field as that needs a POST operation to send to the server. Try sending the id as a URL parameter which can be grabbed using req.param.id or req.query.id, for example

http://example.com/api/users?id=4&token=sdfa3&geo=us

or

http://example.com/api/users/4/sdfa3/us

If you want to get a query parameter ?id=57ada56845b8466147fc35b0, then use req.query

URL:

// GET /prof?id=57ada56845b8466147fc35b0

Markup:

<p><%=users[i].bio %>....<a href="prof?id=<%= users[i]._id %>">Read More</a></p>

Route:

app.get('/prof', function(req, res) {
    var id = req.query.id; // 57ada56845b8466147fc35b0
    mongoose.model('User').findById(id, function(err, user){
        console.log(user);
        res.render('pages/profile', { users: user });
    });
});

For the other parameter usage

URL:

// GET /prof/57ada56845b8466147fc35b0

use req.params.id

Markup:

<p><%=users[i].bio %>....<a href="prof/<%= users[i]._id %>">Read More</a></p>

Route:

app.get('/prof/:id', function(req, res) {
    var id = req.params.id; //57ada56845b8466147fc35b0
    mongoose.model('User').findById(id, function(err, user){
        console.log(user);
        res.render('pages/profile', { users: user });
    });
});

Upvotes: 1

Related Questions