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!!!
modelonItemcome from, it does not exist in the provided class? Do you have custom validators (as otherwise i dont know whymodelwould 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?arrayProp, are you upgrading from a very old typegoose version? If so, do you get any warnings about "Setting Mixed" or have setallowMixed: Severity.ALLOWsomewhere to ignore the warnings?