Mongoose query/schema error only in docker container Mongoose query/schema error only in docker container docker docker

Mongoose query/schema error only in docker container


I am not aware about Docker or AWS Lambda but I have checked your auditSchema and query in simple Node.js express app and same error reproduce at my end as well.

MissingSchemaError:Schema hasn't been registered for model "error". Use mongoose.model(name, schema)

So the problem is: the linkedId property declared in auditSchema as refpath:ref that means it will populate from multiple collections based on the value of a ref property in the document. So when we query Audit.find().populate('linkedId') it fetch all record from Audit collection and populate linkedId for each document. If document contain value ref:error then mongoose try to look for error collection for population and we don't have any error schema or error collection so it will cause above error.

This error will be generated for each document in your Audit collection whose ref field contains value other than error as well. Mongoose needed all schema/collection exist which you've specified as value of ref.

To solve this problem you need to define collection for each value you've provided in ref property of Audit documents

Please refer this to know more about refpath.