【问题标题】:EJS: How can I render two or more populated colletions from mongoose in EJS fileEJS:如何在 EJS 文件中呈现来自猫鼬的两个或多个填充集合
【发布时间】:2018-11-23 12:30:13
【问题描述】:

我有一个与在 EJS 中渲染来自 mongoDB 的两个或更多填充集合相关的问题。

为了传递我的“问题”的最丰富的上下文,我将在这里与大家分享我的一些代码:模型、路由和视图。

模型架构

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>

显然这是错误的,但我不知道如何在不添加第二个 forEach 语句的情况下为每个运动员或团队填充“教练姓名”,这也行不通。

顺便说一句,我阅读了很多关于这个问题的信息,但我找不到解决它的最佳方法......看起来 异步函数 在这里是一种可能性,但老实说,我'我不确定。

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

【问题讨论】:

    标签: javascript node.js express mongoose ejs


    【解决方案1】:

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

    如果代码下面总是有一位教练应该可以工作:

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

    【讨论】:

    • 非常感谢!!!插入数组语法 [0] 实际上起到了作用。+1 对您有帮助...不幸的是,我的声誉不够高,无法显示我的投票。再次感谢。
    【解决方案2】:

    您的对象存在 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>
    

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

    那应该没问题

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 1970-01-01
      • 2016-12-22
      • 2016-07-23
      • 1970-01-01
      • 2014-03-31
      • 2021-09-26
      • 2012-01-29
      • 2013-06-05
      相关资源
      最近更新 更多