Skip to content

Commit

Permalink
Fixed exception where a null element within an array would be attempt…
Browse files Browse the repository at this point in the history
…ed to be populated.

Prior to this change, if there was a wild NULL object in an array within a document, mongoose would attempt to populate it, and would crash. 

For example, if we had a schema containing founders array in such a way - 
  founders: [
    {
      legal_form: { type: mongoose.Schema.Types.ObjectId, ref: "Legal_Form" },
      name: String,
      identification_code: String,
      ownership_percentage: Number,
    },
  ],


and our database contained a document with founders looking like following - 

[
  {
    name: ''name",
    identification_code: ''id_code",
    ownership_percentage: 51.5,
    _id: new ObjectId("63e1fc49d4ff7ce99a3a3faf")
  },
  null
]

Then following error would be thrown - 
TypeError: Cannot read properties of null (reading 'populated')
  • Loading branch information
ZSabakh authored Sep 8, 2023
1 parent 5eb374d commit fbc01b8
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/helpers/populate/markArraySubdocsPopulated.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ module.exports = function markArraySubdocsPopulated(doc, populated) {

if (utils.isMongooseDocumentArray(val)) {
for (let j = 0; j < val.length; ++j) {
val[j].populated(rest, item._docs[id] == null ? void 0 : item._docs[id][j], item);
if(val[j]){

Check failure on line 41 in lib/helpers/populate/markArraySubdocsPopulated.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected space(s) after "if"

Check failure on line 41 in lib/helpers/populate/markArraySubdocsPopulated.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing space before opening brace
val[j].populated(rest, item._docs[id] == null ? void 0 : item._docs[id][j], item);
}
}
break;
}
Expand Down

0 comments on commit fbc01b8

Please sign in to comment.