0

i'm learning mongoose right now and i got a problem when i'm trying to populate players property in Match collection from the Player collection, the players property returns an empty array, I looked for a solution everywhere and can't make it to work.

This is part of Match Schema:

  ...
  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
  }
);

and the Player Schema:

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");

Populate function:

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);
  }
);
});

Player output:

[
  {
    "_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
  }
]

The Match Output:

 {
  "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
}

Please help me understand why the array of players in Match output is empty, Thanks

haiman
  • 1
  • https://stackoverflow.com/questions/29078753/how-to-reference-another-schema-in-my-mongoose-schema look at that jou not `referencing` or `pushing` the array objects – Ylama Nov 08 '19 at 07:23
  • cool man maybe just post what you fixed, and mark it as solved, someone else might just sit with the same problem one day. – Ylama Nov 08 '19 at 09:28

0 Answers0