EJS:如何在EJS文件中从mongoose渲染两个或多个填充的集合

时间:2018-06-13 20:09:27

标签: javascript node.js express mongoose ejs

我遇到了与EJS中两个或多个填充的集合中的渲染相关的问题。

为了传递我的问题"的最丰富的背景,我将在这里与您分享我的一些代码:模型,路线和视图。

模型架构

var mongoose = require("mongoose");

var playerSchema = mongoose.Schema({
    namePlayer:         String,
    email:              String,
    playerTeam: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref:  "Team"
        },
        nameTeam: String
    }
});

var coachSchema = mongoose.Schema({
        nameCoach:  String,
        email:      String,
        nameTeams: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Team"
        }   
    ]
});

var teamSchema = mongoose.Schema({
    nameTeam: String,
    teamPlayers: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Player"
        }
    ],
    teamCoaches: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Coach"
        }       
    ]   
});

module.exports = mongoose.model("Player", playerSchema);
module.exports = mongoose.model("Coach", coachSchema);
module.exports = mongoose.model("Team", teamSchema);

路由器架构

var express = require("express");
var passport = require("passport");
var router  = express.Router();
var Player = require("../models/player");
var Team = require("../models/team");
var Coach = require("../models/coach");

// Find Team by ID
router.get("/resultTeam", function (req, res, next) {

    var teamId = req.query.team;

    Team.findById(teamId).populate("teamPlayers").populate("teamCoaches").exec(function(err, currentTeam) {   
        if(err){
           console.log(err);
        } else {
           console.log(currentTeam);
           res.render("./player/resultTeam", {teams: currentTeam });
        }
    });
});

的console.log(currentTeam)

{ teamPlayers: 
   [ { equipe: [Object],
       _id: 5b20023443589331491e0903,
       namePlayer: 'Zico',
       email: 'lorem@ipsum.com',
       __v: 0 },
     { equipe: [Object],
       _id: 5b215e6460adc33528d29f06,
       namePlayer: 'Pelé',
       email: 'lorem@ipsum.com',
       __v: 0 } ],
  teamCoaches: 
   [ { equipes: [Object],
       _id: 5b1f063b999c3c285aa9f3e7,
       nameCoach: 'Harrison Ford',
       email: 'lorem@ipsum.com',
       __v: 1 } },
  _id: 5b1f061c91b4a7284b6d4945,
  nameTeam: 'Real Madrid',
  __v: 1 }

在这里粘贴了我的所有代码和预期输出后,我现在想要实现的是在我的前端(ejs文件)中展示了关于我的"团队"的一些基本信息:

查看文件

<thead>
    <tr>
        <th><i class="fa fa-image"></i></th>
        <th><i class="fa fa-star"></i> Athlete</th>
        <th class="hidden-phone"><i class="fa fa-question-circle"></i> Coach</th>
        <th><i class="fa fa-bookmark"></i> Email</th>
        <th></th>
    </tr>
</thead>
<tbody>

    <% teams.teamplayers.forEach(function(player) { %>    
    <tr>
        <td></td>
        <td><a href="basic_table.html#"><%= player.namePlayer %></a></td>
        <td class="hidden-phone"><%= coach.coachName %></td>
        <td><%= player.email %></td>
          <td>
            <form style="display: inline" action="/player/<%= player._id %>" method="GET">
               <button class="btn btn-default btn-xs"><i class="fa fa-search-plus"></i></button>
            </form>
        </td>
    </tr>   
    <% }); %>
</tbody>

使用最后一个代码(ejs视图)一切正常,直到我添加这一行:

<td class="hidden-phone"><%= coach.coachName %></td>

显然这是错的,我不知道如何填充&#34;教练姓名&#34;对于每个单独的运动员或团队而言,没有给出第二个forEach法则,这也不会起作用。

顺便说一句,我对这个问题反应很多,我找不到解决问题的最佳方法......看起来像 async function 是可能的,但老实说,我不确定。

可能解决方案比我想象的要容易,但是在13个小时后,我无法自己解决这个问题。

2 个答案:

答案 0 :(得分:0)

coach对象不存在。但是你有teamCoaches,这是一个数组。不确定是否可以有多个教练,如果您应该调整逻辑以循环遍历teamCoaches,就像循环teamPlayers一样。

如果下面的代码总是一个教练应该工作:

<td class="hidden-phone"><%= teamCoaches[0].nameCoach %></td>

答案 1 :(得分:0)

您的对象是 currentTeam,您应该从那里获取对象属性,如下所示:

    <tbody>

    <% currentTeam.teamplayers.forEach(function(player) { %>    
    <tr>
        <td></td>
        <td><a href="basic_table.html#"><%= player.namePlayer %></a></td>
        <td class="hidden-phone"><%= coach.coachName %></td>
        <td><%= player.email %></td>
          <td>
            <form style="display: inline" action="/player/<%= player._id %>" method="GET">
               <button class="btn btn-default btn-xs"><i class="fa fa-search-plus"></i></button>
            </form>
        </td>
    </tr>   
    <% }); %>
</tbody>

在这一行 <td class="hidden-phone"><%= coach.coachName %></td> 请像这样编辑它:<td class="hidden-phone"><%= player.coachName %></td>

那应该没问题