From fbc01b8a53a039e08142660379819a86101ca560 Mon Sep 17 00:00:00 2001 From: Zurab Sabakhtarishvili Date: Fri, 8 Sep 2023 00:04:26 -0500 Subject: [PATCH] Fixed exception where a null element within an array would be attempted 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') --- lib/helpers/populate/markArraySubdocsPopulated.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/helpers/populate/markArraySubdocsPopulated.js b/lib/helpers/populate/markArraySubdocsPopulated.js index 28b19a1989a..a22605a1d78 100644 --- a/lib/helpers/populate/markArraySubdocsPopulated.js +++ b/lib/helpers/populate/markArraySubdocsPopulated.js @@ -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]){ + val[j].populated(rest, item._docs[id] == null ? void 0 : item._docs[id][j], item); + } } break; }