无法传递对象节点路径和渲染网页?

问题描述 投票:0回答:2

我的工作与node.js一个express.jsproject。我跑的路线和渲染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%>
and my route code is:

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

  });
});
javascript html node.js express ejs
2个回答
0
投票

我想有一个在您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>
    <% }); %>

0
投票
<% if (typeof team === "undefined" ) { %> 
// do smth
<%} else {%>
// do smth
<%}%>

根据一些条件,你得到teamundefined,这意味着当你正在GET请求,没有被发送到你的ejs文件,但在某些情况下,你得到teamdefined,所以,你必须隐式实现这两种情况

© www.soinside.com 2019 - 2024. All rights reserved.