在 express js 中使用隐藏字段
using hidden fields in express js
我正在尝试传递隐藏 ID 字段的值并使用它从 mongodb 中检索记录并将其显示在配置文件页面上,在索引 page.This 上单击 "read more" 之后 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 -->
<% } %>
这里是 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>
和快递路线,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});
});
});
这给我一个错误"Cannot read property 'username' of null.."
我错过了什么?
您正在执行 GET
操作,但您正试图通过使用 req.body.variable_name
获取的 POST
参数发送 id
值。
在这种情况下,您不需要隐藏字段,因为它需要 POST
操作才能发送到服务器。尝试将 id
作为 URL 参数发送,可以使用 req.param.id
或 req.query.id
获取,例如
http://example.com/api/users?id=4&token=sdfa3&geo=us
或
http://example.com/api/users/4/sdfa3/us
如果要获取查询参数?id=57ada56845b8466147fc35b0
,则使用req.query
URL:
// GET /prof?id=57ada56845b8466147fc35b0
标记:
<p><%=users[i].bio %>....<a href="prof?id=<%= users[i]._id %>">Read More</a></p>
路线:
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 });
});
});
其他参数用法
URL:
// GET /prof/57ada56845b8466147fc35b0
使用req.params.id
标记:
<p><%=users[i].bio %>....<a href="prof/<%= users[i]._id %>">Read More</a></p>
路线:
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 });
});
});
我正在尝试传递隐藏 ID 字段的值并使用它从 mongodb 中检索记录并将其显示在配置文件页面上,在索引 page.This 上单击 "read more" 之后 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 -->
<% } %>
这里是 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>
和快递路线,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});
});
});
这给我一个错误"Cannot read property 'username' of null.."
我错过了什么?
您正在执行 GET
操作,但您正试图通过使用 req.body.variable_name
获取的 POST
参数发送 id
值。
在这种情况下,您不需要隐藏字段,因为它需要 POST
操作才能发送到服务器。尝试将 id
作为 URL 参数发送,可以使用 req.param.id
或 req.query.id
获取,例如
http://example.com/api/users?id=4&token=sdfa3&geo=us
或
http://example.com/api/users/4/sdfa3/us
如果要获取查询参数?id=57ada56845b8466147fc35b0
,则使用req.query
URL:
// GET /prof?id=57ada56845b8466147fc35b0
标记:
<p><%=users[i].bio %>....<a href="prof?id=<%= users[i]._id %>">Read More</a></p>
路线:
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 });
});
});
其他参数用法
URL:
// GET /prof/57ada56845b8466147fc35b0
使用req.params.id
标记:
<p><%=users[i].bio %>....<a href="prof/<%= users[i]._id %>">Read More</a></p>
路线:
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 });
});
});