I've been developing a straightforward Express app with ES6. In the process of creating a schema and model for Mongoose, I encountered an issue with the following syntax:
import mongoose, { Schema } from 'mongoose';
const PostSchema = new Schema(
{
userId: {
type: Schema.Types.ObjectId,
required: true,
ref: 'User'
},
video: {
type: String,
required: true
},
location: {
type: { type: String },
coordinates: []
}
},
{ timestamps: true }
);
PostSchema.index({ location: '2dsphere' });
const Post = mongoose.model('Post', PostSchema);
export default Post;
This resulted in the error message:
TypeError: _mongoose.Schema is not a constructor
.
However, when I switch to a different syntax like this, it functions correctly:
import mongoose from 'mongoose';
const { Schema } = mongoose;
...
In addition, here is my .babelrc
configuration:
{
"presets": [
"@babel/preset-env"
],
"plugins": [
"@babel/plugin-transform-runtime"
]
}
Could there be an issue with my import approach or Babel setup? Appreciate any insights.