【发布时间】:2019-03-20 02:19:36
【问题描述】:
我一直在使用 ES6 开发一个简单的 Express 应用程序。在为 Mongoose 创建模式和模型时,我使用以下语法:
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;
它会产生这个错误:TypeError: _mongoose.Schema is not a constructor。
当我使用这种语法时,它可以工作:
import mongoose from 'mongoose';
const { Schema } = mongoose;
...
这是我的.babelrc:
{
"presets": [
"@babel/preset-env"
],
"plugins": [
"@babel/plugin-transform-runtime"
]
}
我的导入样式或 Babel 配置有什么问题吗? 谢谢。
【问题讨论】:
-
我很确定导入语法没有解构。
标签: javascript express mongoose ecmascript-6