我的工作与node.js
一个express.js
project。我跑的路线和渲染EJS页面,并通过team
对象吧。当我做了console.log(team)
它显示了球队的细节,但它给undefined
价值<%= team.member.name %>
虽然它的存在。我怎样才能解决这个问题。我附上console.log(team)
的截图和我的EJS代码了。请帮助This is the embedded screenshot
我的EJS文件是:
<% include ./partials/header%>
<% console.log("yup"+team)%>
<table class="table-striped table table-bordered">
<tr class="thead-dark">
<th>Name</th>
<th>Year</th>
<th>Skills</th>
<th>Leader</th>
</tr>
<% for(i = 0; i < team.members.length; i++){
var member = team.members[i];%>
<tr>
<td><%= member.name %></td>
<td><%= member.year %></td>
<td>
<ul>
<% for(var i = 0; i < member.skills.length; i++){ %>
<li>
<%= member.skills[i];%>
</li>
<% } %>
</ul>
</td>
<td><% if(member.isLeader) { %>
Yes
<% } else { %>
No
<% } %>
</td>
</tr>
<% } %>
</table>
<% include ./partials/footer%>
router.get("/:id/view/:challengeid/:username", isLoggedIn, function(req, res){
Mentor.findById(req.params.id, function(err, mentor){
mentor.mentorChallenges.forEach(function(chall){
var k = 0;
for(var i = 0; i < chall.applicants.length; i ++){
if(chall.applicants[i] == req.params.username){
k = 1;
break;
}
}
if(k === 1){
Team.find({username: req.params.username}, function(err, team){
res.render("teamDetails", {team: team});
});
}
});
});
});
我想有一个在您for
循环有问题。你尝试forEach
循环?
尝试使用这样的:
<% team.members.forEach(member => { %>
<tr>
<td><%= member.name %></td>
<td><%= member.year %></td>
<td>
<ul>
<% member.skills.forEach(skill => { %>
<li>
<%= skill%>
</li>
<% }); %>
</ul>
</td>
<td><% if(member.isLeader) { %>
Yes
<% } else { %>
No
<% } %>
</td>
</tr>
<% }); %>
<% if (typeof team === "undefined" ) { %>
// do smth
<%} else {%>
// do smth
<%}%>
根据一些条件,你得到team
为undefined
,这意味着当你正在GET
请求,没有被发送到你的ejs
文件,但在某些情况下,你得到team
为defined
,所以,你必须隐式实现这两种情况