claytongulick/mongoose-json-patch

TypeError: Cannot read property 'options' of undefined

Opened this issue · 0 comments

Hello,

We are running into this issue, and I thought I'd write to you after spending some time examining both side of the code:

TypeError: Cannot read property 'options' of undefined
    at JSONPatchMongoose.populatePath (/vlab_shared/vl-models/node_modules/mongoose-patcher/json_patch_mongoose.js:376:33)
    at next (/vlab_shared/vl-models/node_modules/mongoose-patcher/json_patch_mongoose.js:86:28)
    at JSONPatchMongoose.apply (/vlab_shared/vl-models/node_modules/mongoose-patcher/json_patch_mongoose.js:93:23)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at runNextTicks (internal/process/task_queues.js:62:3)
    at processImmediate (internal/timers.js:434:9)
    at model.schema.methods.jsonPatch (/vlab_shared/vl-models/node_modules/mongoose-patcher/index.js:12:9)

Patch has this form:

[{"op":"replace","path":"/version","value":6},{"op":"replace","path":"/modification_date","value":"2023-07-21T13:30:11.400Z"},{"op":"replace","path":"/accounts/main","value":["60a2facac1f8e816afb0645d","60a2facac1f8e87e26b0645e","62d525acf4e903001169d7d0"]}]

And the relevant part of the object is this:

        "accounts" : {
                "main" : [
                        "60a2facac1f8e816afb0645d",
                        "60a2facac1f8e87e26b0645e"
                ],
                "linked" : {
                        "60a2facac1f8e816afb0645d" : 2,
                        "62d525acf4e903001169d7d0" : 2
                }
        }

It seems like the array is causing that issue and I know that we've had to change the schema after running into unrelated issues:

const mongoose = require('mongoose');

module.exports = {
    type: {
        main: [String],
        linked: mongoose.Mixed,
        old: mongoose.Mixed
    },
    set: function (newVal) {
        if (!this.accounts.old) {
            this.accounts.old = {};
        }

        this.accounts.old.main = (this.accounts.main || []).slice();
        this.accounts.old.linked = Object.assign({}, this.accounts.linked);

        return newVal;
    },
    default: {
        main: [],
        linked: {}
    },
    _id: false
};

This is a POJO schema imported as vlAccounts and used in the document that is patched like that:

        accounts: vlAccounts,

Upon inspection it seems that the accounts array loaded from the document is lacking a $schema() method as well as a _schema property. We tried to understand which part of Mongoose is responsible for populating this method and property but at this stage I thought I'll create an issue here - any help would be appreciated. I don't know for how many years we have been using mongoose-patcher but it's certainly a core part of our application, thank you for maintaining it. I appreciate the time you would spend reading and replying to this and I will forward my issue to the project owner with a recommendation to make a donation towards the project.

The version we are using is 0.2.15.

Cheers,
Svetoslav