我现在正在学习猫鼬,当我尝试从Player集合中填充Match集合中的players属性时,我遇到了一个问题,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
},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
}
请帮助我理解为什么Match输出中的玩家数组为空,谢谢