ksil
10
2019-05-16 01:10:42
2
97

node.js ejs 뷰에서 에러납니다 ㅜ Cannot read property '0' of undefined


웹 개발은 초보라 아직 많이 미숙합니다 ㅠ


[router]

router.post("/quiz", function(req, res, next) {
  console.log("/quiz 호출됨");
 
  var paramQuestion = 'already got';
  var paramAnswer = req.param('answer');
  var userId = req.user._id;
  console.log(req.user);
  console.log(req.user.username);
  console.log(req.user._id);
 
  if(db) {
    authUser(db, paramQuestion, paramAnswer, function(err, docs) {
      if(err) {throw err;}
 
      if(docs) {
        var User = db.collection('users');
        User.updateOne({"_id":userId}, {$inc: {point: 89}});
 
        var field = "userinfo";
        var username = req.user.username;
        var name = req.user.name;
        var obj = {
          username : username,
          name : name
        };
 
        client.hset(field, "username", obj.username);
        client.hset(field, "name", obj.name);
 
        var board = "leaderboard";
        var score = 89;
        var start = 0;
        var end = 5;
 
        client.zincrby(board, parseInt(score), username, function(err, reply){
          if(err){
            console.log("error");
            return;
          } else{
            console.log("ranking : " + reply);
          }
        });
 
        client.ZREVRANGE(board, start, end, "WITHSCORES", function(err, members){
          console.log('members', members);
          var scores = [];
          scores.push(members);
          console.log(scores);
 

// 이 부분에서 score 배열에 ['username', 'score', 'username2', 'score2' ...] 이런식으로 들어가게 되는데

랭킹 뷰에서 보여주려고할때

          if(err){
          console.log('err', err);
          console.log('scores', scores);
          res.render('challenges/rank', {scores:scores, error:false});
          }
        });


[rank.ejs]

<html>
  <head>
    <% include ../partials/head %>
  </head>
  <body>
    <% include ../partials/nav %>
    <div class="container home home-welcome">
      <div class="jumbotron">
          <span class="border">
          <h2 class="rank_h2">Rank</h2><br><br>
        <div class="rank_custom1">
          <table class="table table-striped table-hover table-dark">
              <thead>
                <tr>
                  <th scope="col">Rank</th>
                  <th scope="col">Username</th>
                  <th scope="col">Score</th>
                </tr>
              </thead>
              <tbody>
                <% for(i=0;members=scores[i], i<scores.length;i+=2) { %>

// 이 부분에서  Cannot read property '0' of undefined 이 오류가 나는데 어떻게 해야 할까요 ㅜ

               <tr>

                  <th scope="row">1</th>
                  <td><%= members.scores[i] %></td>
                  <td><%= members.scores[i+1] %></td>
                </tr>
                <% } %>


        </div>
          </span>
 
              </tbody>
          </table>
      </div>
    </div>
  </body>
</html> 

0
0
  • 답변 2

  • ksil
    10
    2019-05-16 01:12:23

    위에 잘 안보이는데

    <% for(i=0;members=scores[i], i<scores.length;i+=2) { %>
                    <tr>
                      <th scope="row">1</th>
                      <td><%= members[scores[i]] %></td>
                      <td><%= members[scores[i+1]] %></td>
                    </tr>
                    <% } %>

     이것으로 랭킹 뷰를 보여주고자 하는데 안됩니다 ㅠ

    0
  • ksil
    10
    2019-05-16 01:13:01

     <% for(i=0;members=scores[i], i<scores.length;i+=2) { %> 
                    <tr> 
                      <th scope="row">1</th> 
                      <td><%= members[scores[i]] %></td> 
                      <td><%= members[scores[i+1]] %></td> 
                    </tr> 
                    <% } %> 



    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.