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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0