Model / Company / index.js:
import {Schema} from 'mongoose';
import CompanyAddressSchema from '../CompanyAddress';
import mongoose from 'mongoose';
const companySchema = new Schema({
name: {type: Schema.Types.String, minlength: 1, maxlength: 80, required: true},
type: {type: Schema.Types.String, minlength: 1, maxlength: 80, required: true},
companyAddress: [CompanyAddressSchema]
}, { timestamps: true });
const Company = mongoose.model(‘Company’, companySchema);
export default Company;
CompanyService(在文件顶部调用):
import Company from '../models/Company';
公司服务(获取数据的查询:
let company = await Company.findOne({_id: Schema.Types.ObjectId(id)});
.babelrc:
{
"presets": ["env"],
"plugins": ["transform-object-rest-spread",
"transform-async-to-generator",
"transform-es2015-arrow-functions",
"transform-builtin-extend",
"add-module-exports",
["transform-runtime", {
"polyfill": false,
"regenerator": true
}]]
}
这里的问题是公司最终未定义。我不确定如果在文件顶部声明它怎么可能不确定。可能是什么原因导致的?解决方法是什么?
更新:我注意到,如果我将导入语句替换为
“让公司= require(../ models / Company).default”
我得到了正确的物体。如何使用import语句实现这一目标?