我不知道该怎么解释,但我会试一试。
我在MongoDB中有两个表。它们是:
-Games
-Players
游戏表的两个数据:
{赛季: 4,比赛: 1}
{赛季: 4,比赛: 2}
播放器表的四个数据:
{赛季: 4,比赛: 1,球员:圣优素福,积分: 100}
{赛季: 4,比赛: 1,球员:圣艾萨克,积分: 80}
{赛季: 4,比赛: 2,球员:圣优素福,积分: 90}
{赛季: 4,比赛: 2,球员:圣艾萨克,积分: 100}
所以,他们就是数据。我想把它们写到ejs文件,如下所示:
游戏1:
St.Yusuf|100分
圣艾萨克|80分
第二场比赛:
圣艾萨克|100分
St.Yusuf|90分
但当我尝试编写它们时,它们得到的内容是这样的:
游戏1:
St.Yusuf|100分
圣艾萨克|80分
圣艾萨克|100分
St.Yusuf|90分
第二场比赛:
St.Yusuf|100分
圣艾萨克|80分
圣艾萨克|100分
St.Yusuf|90分
*点的排序无关紧要。这种情况下,我想写的球员只有相同的游戏号码,而不是所有的游戏彼此…
那么,如何修复它呢?后端代码如下:
Games.find({}, (err, gamesData) => {
Players.find({}, (err, playersData) => {
res.render('score-board', {games: gamesData, players: playersData })
})
})以下是ejs代码:
<% games.forEach(game => { %>
<table>
<thead>
<tr>
<th>Player</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<% players.forEach(player => { %>
<tr>
<td class="player-name"><%- player.player %></td>
<td class="player-name"><%- player.points%></td>
</tr>
<% }) %>
</tbody>
</table>
<% }) %>以下是游戏表格的结构:
const mongoose = require('mongoose')
const Schema = new mongoose.Schema({
season: {type:String},
game: {type:String}
}, {timestamps:true})
module.exports = mongoose.model('games', Schema)发布于 2021-05-29 22:49:42
为了达到您想要的结果,您应该将您的表重新构造为如下所示:
{
"Games" : [
{
"season":4,
"game":1,
"Players": [
{
"player" :"St. Yusuf",
"points" : 100
},
{
"player" :"St. Isaac",
"points" : 80
}
]
},
{
"season":4,
"game":2,
"Players": [
{
"player" :"St. Yusuf",
"points" : 90
},
{
"player" :"St. Isaac",
"points" : 100
}
]
}
]
}您的.ejs文件将如下所示:
<% games.forEach(game => { %>
<table>
<thead>
<tr>
<th>Player</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<% games.Players.forEach(player => { %>
<tr>
<td class="player-name"><%- games.Players.player %></td>
<td class="player-name"><%- games.Players.points%></td>
</tr>
<% }) %>
</tbody>
</table>
<% }) %>游戏表架构:
const mongoose = require('mongoose')
const Schema = new mongoose.Schema({
season: {type:String},
game: {type:String},
Players: [
{
player: {type:String},
points: {type:Number}
}
]
}, {timestamps:true})
module.exports = mongoose.model('games', Schema);https://stackoverflow.com/questions/67752176
复制相似问题