0

I am working on migrating the project to a new version. Stack: express 5, mongoose 8, typescript 5.8, typegoose 12

I looked for more information and would now like to clarify the cause of the problem:

My guess is that the validator in the older version of mongoose did not check for the existence of an entity by the _id field when saving nested arrays of objects, which was mistakenly not set to "false", and in the older version of typegoose such fields were marked as arrayProp, maybe this still mattered. Because if I read the stack trace correctly, which the error displays to me, the validator does not understand what entity _id is bound to, since this model really does not exist, which is what the error says. The "crutch" solution => save({ validateBeforeSave: false }); helped,basically I made an "update" that doesn't validate.

export class Item {
    @prop({ref: () => UserSchema })
    public user: UserSchema;
    @prop()
    public someField: 'ok'
}

export class MyModel {
@prop({type: () => [Items]})
public items: Items[]
@prop()
public status: 'active'

I got an error when try change 1 filed and save:

MyModel.find({}).then((items) => {
    for (let i = 0; i < items.length; i++) {
        const x = items[i];
        ...some logic, all works
        x.status = 'new'
        x.save()
    }
)

My error:

ValidationError: MyModelSchema validation failed: items.0: Cannot read properties of undefined (reading 'model')

According to the mongoose documentation, I understood that the problem is with saving the subdocument, the DB on the old version of mongo. Is it possible to do something about this, because migrating everything will be almost impossible. Many thanks!!!

2
  • I have no clue yet what the issue could be with the provided information, so some various questions: Is the stack you provided the old*(before migration) or *new stack, if new could you provide the old one? Where does field model on Item come from, it does not exist in the provided class? Do you have custom validators (as otherwise i dont know why model would be accessed)? Could you provide full example code? (including data pre-existing data if necessary) And or what you old code / schema was before migration? Commented Sep 8 at 16:10
  • Additionally, as you mentioned arrayProp, are you upgrading from a very old typegoose version? If so, do you get any warnings about "Setting Mixed" or have set allowMixed: Severity.ALLOW somewhere to ignore the warnings? Commented Sep 8 at 16:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.