带有 ts 的 nodeJS : 模块在本地声明组件,但不导出
nodeJS with ts : module declares component locally, but it is not exported
在我的 nodeJS 应用程序中,我有一个 models 和 seeders 文件夹,我创建了一个这样的 address.model.ts 模式:
export {};
const mongoose = require('mongoose');
const addressSchema = new mongoose.Schema({
street: {
type: String,
required: true,
},
number: {
type: String,
required: true,
},
city: {
type: String,
required: true,
},
codePostal: { type: mongoose.Schema.Types.ObjectId, ref: 'codePostal' },
country: {
type: String,
required: true,
},
longitude: {
type: Number,
required: false,
},
latitude: {
type: Number,
required: false,
}
});
const ALLOWED_FIELDS = ['id', 'street', 'number','city', 'codePostal', 'country'];
/**
* @typedef Address
*/
const Address = mongoose.model('Address', addressSchema);
Address.ALLOWED_FIELDS = ALLOWED_FIELDS;
module.exports = Address;
和 addresses.ts 这样的种子:
import faker from 'faker'
import {
Address
} from '../src/api/models/address.model'
export const seedAdresses = async () => {
try {
const quantity = 10
const adresses = []
for (let i = 0; i < quantity; i++) {
adresses.push(
new Address({
street : faker.address.streetName(),
number : faker.address.streetAddress(),
city : faker.address.city(),
country : faker.address.country(),
longitude: faker.address.longitude(),
latitude : faker.address.latitude(),
})
)
}
} catch (err) {
console.log(err);
}
}
seedAdresses()
导入地址时出错:
module '"../src/api/models/address.model"' declare 'Address' locally, but it is not exported.
I don't understand why it's not exported although module.exports = Address; exist in my schema!
问题是您正在使用 ES6 imports/exports 的 CommonJS 导出,在 address.model.ts 中使用 export { Address };
而不是 export {};
。
您还应该考虑使用 import { Schema, model } from "mongoose"
以与 ES6 保持一致。
在我的 nodeJS 应用程序中,我有一个 models 和 seeders 文件夹,我创建了一个这样的 address.model.ts 模式:
export {};
const mongoose = require('mongoose');
const addressSchema = new mongoose.Schema({
street: {
type: String,
required: true,
},
number: {
type: String,
required: true,
},
city: {
type: String,
required: true,
},
codePostal: { type: mongoose.Schema.Types.ObjectId, ref: 'codePostal' },
country: {
type: String,
required: true,
},
longitude: {
type: Number,
required: false,
},
latitude: {
type: Number,
required: false,
}
});
const ALLOWED_FIELDS = ['id', 'street', 'number','city', 'codePostal', 'country'];
/**
* @typedef Address
*/
const Address = mongoose.model('Address', addressSchema);
Address.ALLOWED_FIELDS = ALLOWED_FIELDS;
module.exports = Address;
和 addresses.ts 这样的种子:
import faker from 'faker'
import {
Address
} from '../src/api/models/address.model'
export const seedAdresses = async () => {
try {
const quantity = 10
const adresses = []
for (let i = 0; i < quantity; i++) {
adresses.push(
new Address({
street : faker.address.streetName(),
number : faker.address.streetAddress(),
city : faker.address.city(),
country : faker.address.country(),
longitude: faker.address.longitude(),
latitude : faker.address.latitude(),
})
)
}
} catch (err) {
console.log(err);
}
}
seedAdresses()
导入地址时出错:
module '"../src/api/models/address.model"' declare 'Address' locally, but it is not exported. I don't understand why it's not exported although module.exports = Address; exist in my schema!
问题是您正在使用 ES6 imports/exports 的 CommonJS 导出,在 address.model.ts 中使用 export { Address };
而不是 export {};
。
您还应该考虑使用 import { Schema, model } from "mongoose"
以与 ES6 保持一致。