I have a MongoDB database which I am querying with Mongoose.
I am using a collection audits
to store information about update changes, failed logins and errors.
Audit Model
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema;
const auditSchema = new Schema({
ref: {
type: String,
required: true
},
linkedId: {
type: Schema.ObjectId,
refPath: 'ref'
},
action: {
type: String,
enum: ['insert', 'update', 'delete', 'special']
},
/* ... lots more fields */
});
module.exports = mongoose.model('audit', auditSchema);
Querying this collection in my docker container using the query below:
return Audit.find().sort({date: -1})
.skip(skipInt)
.limit(limitInt)
.populate('user')
.populate('linkedId')
.exec();
I receive the following error:
Schema hasn't been registered for model "error". Use mongoose.model(name, schema)
There isn't an "error" schema however I've checked that all the entries in the collection with a ref
of error don't have a linkedId
populated:
{ref: 'error', linkedId: {$exists: true}} // 0 documents
I don't understand why this works fine on my dev system (win 10, vscode debugging) but errors in both my local docker as well as aws lamda.
I've run a complete re-build of the docker images to try and make sure it wasn't a caching issue but still the error remains.
edit
Tried the two docker files, both exhibit the same behavior, building on a windows 10 machine
FROM node:8-alpine
WORKDIR /usr/app
COPY package.json .
RUN npm i --quiet
COPY . .
RUN npm install pm2 -g
CMD ["pm2-runtime", "./bin/www"]
FROM node:12
WORKDIR /tmp
RUN wget https://downloadarchive.documentfoundation.org/libreoffice/old/5.4.7.2/deb/x86_64/LibreOffice_5.4.7.2_Linux_x86-64_deb.tar.gz -O libo.tar.gz
RUN apt update \
&& apt install -y libxinerama1 libfontconfig1 libdbus-glib-1-2 libcairo2 libcups2 libglu1-mesa libsm6 unzip \
&& tar -zxvf libo.tar.gz
WORKDIR /tmp/LibreOffice_5.4.7.2_Linux_x86-64_deb/DEBS
RUN dpkg -i *.deb
WORKDIR /usr/app
COPY package.json .
RUN rm -rf node_modules
RUN npm install --arch=x64 --platform=linux sharp
RUN npm i --quiet
COPY . .
RUN npm install pm2 -g
CMD ["pm2-runtime", "./bin/www"]
edit 2
I think I have found the difference but not a solution, the container shows mongoose version 5.9.28
however package.json listed ^5.8.3
, updating mongoose
to version 5.9.28
gives the same error on my local installation, however now I'm stuck with the same error and no clue how to solve it.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With