从 findAll 获取数据并将其传递给 ejs 文件

fetching data from findAll and pass it to ejs file

我正在尝试从 mysql 获取数据并将数据传递到 ejs 文件。我想将传递的数据显示为 html 数据表。但它似乎不起作用。 我正在使用 sequelize 和节点 js 和 ejs。

下面是我如何实现 findAll() 方法

router.get('/list', function (req,res, next)  {
player.findAll()
.then( players => {
    console.log(data);
    var data = res.json(players);
    res.render("list", {data:data})

});

});

这里是我想要显示数据的地方。

        <table>
      <thead>
        <tr>
          <th scope="col"></th>
          <th scope="col">Name</th>
          <th scope="col">Last follow up date</th>
          <th scope="col"></th>
          <th scope="col"></th>
          <th scope="col"></th>
        </tr>
      </thead>

      <tbody id="Table-data">
        <%if (typeof data != 'undefined'){ %>
          <% data.forEach(function(row){ %>
        <tr>
          <td> 1 </td>
          <td> <%= row.name %> </td>
          <td> 15 </td>
          <td>
            <a href="./chart">
              <span class="title">
                <svg
                  width="1em"
                  height="1em"
                  viewBox="0 0 16 16"
                  class="bi bi-bar-chart-line"
                  fill="currentColor"
                  xmlns="http://www.w3.org/2000/svg"
                >
                  <path
                    fill-rule="evenodd"
                    d="M11 2a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v12h.5a.5.5 0 0 1 0 1H.5a.5.5 0 0 1 0-1H1v-3a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v3h1V7a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v7h1V2zm1 12h2V2h-2v12zm-3 0V7H7v7h2zm-5 0v-3H2v3h2z"
                  />
                </svg>
                view
              </span>
            </a>
          </td>

          <td>
            <a href="./edit">
              <span class="title">
                <svg
                  width="1em"
                  height="1em"
                  viewBox="0 0 16 16"
                  class="bi bi-pencil-square"
                  fill="currentColor"
                  xmlns="http://www.w3.org/2000/svg"
                >
                  <path
                    d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456l-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"
                  />
                  <path
                    fill-rule="evenodd"
                    d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"
                  />
                </svg>
                edit
              </span>
            </a>
          </td>

          <td>
            <a href="./addEnv">
              <span class="title">
                <svg
                  width="1em"
                  height="1em"
                  viewBox="0 0 16 16"
                  class="bi bi-plus-square"
                  fill="currentColor"
                  xmlns="http://www.w3.org/2000/svg"
                >
                  <path
                    fill-rule="evenodd"
                    d="M14 1H2a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2z"
                  />
                  <path
                    fill-rule="evenodd"
                    d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"
                  />
                </svg>
                add enviroment
              </span>
            </a>
          </td>

        </tr>
        <% })} else{ %>
          <tr>
              <td colspan="6">No Record Found</td>
              
            </tr>
          <% } %>
      </tbody>
    </table>

输出总是这样 output

感谢您的提前帮助

尝试将玩家直接传递给模板

router.get('/list', function (req,res, next)  {
  player.findAll()
    .then(players => {
      res.render("list", {data: players})
    });