Я сейчас изучаю Мангуста, и у меня возникла проблема, когда я пытаюсь заполнить свойство Players в коллекции Match из коллекции Player, свойство Players возвращает пустой массив, я везде искал решение и не могузаставить его работать.
Это часть схемы матча:
...
corners_half2: {
type: Number
},
tackels_half: {
type: Number
},
tackels_half2: {
type: Number
},
cycle: {
type: Number
},
home_or_away: {
type: String
},
goals: [
{
minute: {
type: Number
},
name: {
type: String
}
}
],
assist: [
{
minute: {
type: Number
},
name: {
type: String
}
}
],
players: [
{
type: Schema.Types.ObjectId,
ref: "Player"
}
]
},
{
timestamps: true
}
);
и схемы игрока:
const playerSchema = new Schema(
{
name: {
type: String,
required: true
},
lastname: {
type: String,
required: true,
minlength: 2
},
total_time: {
type: Number
},
total_distance: {
type: Number
},
sprint_distance: {
type: Number
},
total_sprint: {
type: Number
},
sprint_avg: {
type: Number
},
goals: {
type: Number
},
assist: {
type: Number
},
cycle: {
type: Number
}
},
{
timestamps: true
}
);
const Player = mongoose.model("Player", playerSchema, "players");
Заполнить функцию:
app.get("/matches/:id", (req, res) => {
Match.findOne({})
.populate("players")
.exec(
(err, doc) => {
if (err) handleError(res, err.message, "Failed to get players");
res.status(200).json(doc);
console.log(req.params);
}
);
});
Вывод игрока:
[
{
"_id": "5dc47f646463a124583006d1",
"name": "test2",
"lastname": "player",
"total_time": 16,
"total_distance": 1.3,
"sprint_distance": 0.3,
"total_sprint": 7,
"sprint_avg": 1,
"goals": 3,
"assist": 3,
"cycle": 1,
"createdAt": "2019-11-07T20:32:36.176Z",
"updatedAt": "2019-11-07T20:32:36.176Z",
"__v": 0
}
]
Вывод матча:
{
"players": [],
"_id": "5dc5006d11fa703798b36f3b",
"team1": "fdsf",
"team2": "fsdfjdsd",
"goal_team1_half": 1,
"goal_team2_half": 2,
"goal_team1_half2": 3,
"goal_team2_half2": 4,
"shots_team1_half": 3,
"shots_team1_half2": 6,
"shots_team2_half": 34,
"shots_team2_half2": 2,
"shots_on_target_half": 3,
"shots_on_target_half2": 2,
"offsides_half": 3,
"offsides_half2": 4,
"corners_half": 3,
"corners_half2": 3,
"tackels_half": 3,
"tackels_half2": 3,
"cycle": 3,
"home_or_away": "home",
"goals": [],
"assist": [],
"createdAt": "2019-11-08T05:43:09.242Z",
"updatedAt": "2019-11-08T05:43:09.242Z",
"__v": 0
}
Пожалуйста, помогите мне понять, почему массив игроков в выводе матча пуст, Спасибо