{"action":"opened","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/6358","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/6358/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/6358/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/6358/events","html_url":"https://github.com/Automattic/mongoose/issues/6358","id":314861193,"number":6358,"title":"Deep Populate sometimes returns 'null'","user":{"login":"felipevignon","id":12388426,"avatar_url":"https://avatars0.githubusercontent.com/u/12388426?v=4","gravatar_id":"","url":"https://api.github.com/users/felipevignon","html_url":"https://github.com/felipevignon","followers_url":"https://api.github.com/users/felipevignon/followers","following_url":"https://api.github.com/users/felipevignon/following{/other_user}","gists_url":"https://api.github.com/users/felipevignon/gists{/gist_id}","starred_url":"https://api.github.com/users/felipevignon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/felipevignon/subscriptions","organizations_url":"https://api.github.com/users/felipevignon/orgs","repos_url":"https://api.github.com/users/felipevignon/repos","events_url":"https://api.github.com/users/felipevignon/events{/privacy}","received_events_url":"https://api.github.com/users/felipevignon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-04-17T00:15:15Z","updated_at":"2018-04-17T00:15:15Z","closed_at":null,"author_association":"NONE","body":"Reporting what seems to be a bug\r\n\r\nSuppose you have 3 collections, Players, Teams and Houses, with the following Schemas:\r\n\r\nPlayerSchema: {\r\n  email: String,\r\n  name: String,\r\n  city: String\r\n},\r\n\r\nTeamSchema: {\r\n  name: String,\r\n  leader: {\r\n    type: mongoose.Types.ObjectId,\r\n    ref: 'Player'\r\n  },\r\n  players: [\r\n    {\r\n      type: mongoose.Types.ObjectId,\r\n      ref: 'Player\r\n    }\r\n  ]\r\n},\r\n\r\nHouseSchema: {\r\n  name: String,\r\n  team: {\r\n    type: mongoose.Types.ObjectId\r\n    ref: 'Team'\r\n  }\r\n}\r\n\r\nThat is: a House is tied to a Team which has some Players (and a captain, who's also a Player) in it.\r\nInside the \"House\" Model, because I have several methods that need the emails of the players tied to that House, I created an internal function to get that, which goes something like this:\r\n\r\nconst getPlayers = house => {\r\n  // returns a promise\r\n  return house\r\n    .populate({\r\n      path: 'team',\r\n      select: 'leader players',\r\n      populate: [\r\n        {\r\n          path: 'leader',\r\n          select: 'email'\r\n        },\r\n        {\r\n          path: 'players',\r\n          select: 'email'\r\n        }\r\n      ]\r\n    })\r\n    .execPopulate()\r\n    .then(housePopulated => {\r\n      // Log my populated house object\r\n      console.log(housePopulated);\r\n      let playerArray = [];\r\n      // GET PLAYERS\r\n      // if team.players is not empty, add each one with an email to my array\r\n      if (housePopulated.team.players && housePopulated.team.players.length > 0) {\r\n        housePopulated.team.players.forEach(player => {\r\n          if (player.email) playerArray.push(player);\r\n        });\r\n      }\r\n      // If there is a leader and they have an email, add them to my array as well\r\n      if (housePopulated.team.leader && housePopulated.team.leader.email)\r\n        playerArray.push(housePopulated.team.leader);\r\n      // return my final array\r\n      return playerArray;\r\n    })\r\n};\r\n\r\nAnd then I have methods which use that function, such as this example:\r\n\r\nHouseSchema.methods.logEveryone = function() {\r\n  this.getPlayers\r\n    .then(console.log)\r\n    .catch(console.error);\r\n};\r\n\r\nNow, when invoking logEveryone() with a house that I know has a team that has players in it, I sometimes get an error of \"cannot read property 'players' of null\" because my 'housePopulated' object is returning with a 'team: null', even though I know for a fact that the 'team' field in my house has a valid ObjectId with a corresponding 'team' document in my database.\r\n\r\nThis is not easy to reproduce as I couldn't figure out exactly what triggers it - most times my logEveryone method works perfectly as expected, but inevitably it will fail.\r\n\r\nRunning on:\r\nNode 8.10.0\r\nMongoose 5.0.14"}}
{"actor":{"display_login":"felipevignon"}}