Home > Software design >  Mongoose cant save with subdocument array in schema
Mongoose cant save with subdocument array in schema

Time:03-09

I made this mongoose schema with a nested array of subdocuments:

const deliverySchema = new db.Schema({
   price: Number
})

const suppliersSchema = new db.Schema({
    name: String,
    deliveries: [deliverySchema]
})

exports.Suppliers = db.model('Suppliers', suppliersSchema)
const suppliers = new Suppliers({name})

await suppliers.save()

But when I try to save a document i get this error:

TypeError: Cannot read properties of undefined (reading 'length')
    at cloneArray...

If I remove the subdocument from the schema the document gets saved without issues.

Why can't I save the document with this schema?

CodePudding user response:

I can't say exactly where the problem was but I decided to reinstall all npm packages and the problem was solved.

So for anyone who encounters this problem and want to avoid medication just remove your npm folder and reinstall. It might be a solution.

  • Related