Mongoose Schema created using CommonJS not working again after porting to Module
See original GitHub issuePrerequisites
- I have written a descriptive issue title
Mongoose version
6.7.1
Node.js version
14.19.1
MongoDB version
4.11.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
MacOS Monterey, 12.4
Issue
I started out my Mongoose Schema declarations using CommonJS as shown below and populate was working perfectly.
For CommonJS I have:
author.js
const mongoose = require('mongoose');
const AuthorSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
first_name: String,
last_name: String,
email: String,
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
});
module.exports = mongoose.model('Author', AuthorSchema);
comment.js
const mongoose = require('mongoose');
const CommentSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
text: String,
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Author'
}
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
});
module.exports = mongoose.model('Comment', CommentSchema);
and the below query worked flawlessly when using CommonJS
let comments = await Comment.find({ author: authorId })
.populate("author", 'first_name last_name _id').limit(10);
Now, my team wanted us to move away from CommonJS to modules and so I added type:module
to my package.json
file and attempted to refactor the schema declarations above to the following:
new_author.js
import mongoose from 'mongoose';
const AuthorSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
first_name: String,
last_name: String,
email: String,
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
});
export default mongoose.model('Author', AuthorSchema);
and
new_comment.js
import mongoose from 'mongoose';
const CommentSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
text: String,
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Author'
}
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
});
export default mongoose.model('Comment', CommentSchema);
and all of a sudden the following query no longer works
let comments = await Comment.find({ author: authorId })
.populate("author", 'first_name last_name _id').limit(10);
The error I keep getting back is
MissingSchemaError: Schema hasn't been registered for model "Author".
This is unbelievable. As you can see above, nothing changed other than using imports
and export default
statements.
Issue Analytics
- State:
- Created 10 months ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Mongoose Schema created using CommonJS not working ...
Mongoose Schema created using CommonJS not working again after porting to Module ; const mongoose = require ; const mongoose = require ;...
Read more >CommonJS: module functions not available when re-exporting ...
Just create a mongoose model and try calling any MongoDB methods on it. Models/Device.js: var mongoose = require('mongoose'); var deviceSchema = new mongoose....
Read more >useDB doesn't allow to populate. · Issue #11003 - GitHub
When we are using the useDB approach to achieve the multitenancy then the mongoose doesn't have information about the nested schema's.
Read more >Node.js now supports named imports from CommonJS ...
The good news is that, as the error message says, you can always import the default export from a CJS module in an...
Read more >Mongoose v6.8.2: Schemas
When you create a new document with the automatically added _id property, Mongoose creates a new _id of type ObjectId to your document....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I changed ref to
authors
and issue still persistsThis is really weird. The
authors
collection already has over 1k documents in it so how is it possible that its schema has not been created?Please note that this issue doesn’t happen if both services move back to CommonJS. It seems to only happen when we switched to module