【发布时间】:2016-12-21 13:12:42
【问题描述】:
我正在尝试传递隐藏 id 字段的值并使用它从 mongodb 中检索记录并将其显示在配置文件页面上,点击索引页面上的“阅读更多”后。这是 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});
});
});
这给了我一个错误“无法读取 null 的属性'用户名'..”
我错过了什么?
【问题讨论】:
-
你可以试试
users[i].bio.username吗?如果我没记错users[i].bio为空。 -
"Cannot read property 'username' of null.." 告诉我们 mongod 没有返回结果。尝试控制台隐藏的发布值是什么结果。
-
索引页面正在运行,users[i].bio 不为空。只有当我点击“阅读更多”时,我才会得到错误“无法读取 null 的属性'用户名'..”
-
它返回 undefine @owaishanif786
标签: javascript mongodb express ejs