我正在使用nest,在设置猫鼬插件之前,我需要调用另一个服务来获取密钥,我试图在main.ts
中启动mongoose插件,但是它不起作用,下面是我做的
import { Document } from 'mongoose';
export class TestSchema extends Document{
readonly test: String;
}
export const initMongoosePlugin = () => {
TestSchema.plugin(plugin, {
fields: ['test'],
secret: global['KEY'], // init it in main.ts
});
};
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const appService: AppService = app.get(AppService);
global['KEY'] = await appService.getKey();
// init Mongoose Plugin
initMongoosePlugin();
app.use(requestIp.mw());
await app.listen(3000));
}
bootstrap();
答案 0 :(得分:-1)
您收到错误消息还是只是未设置?您是否尝试过从注入架构的服务构造函数中调用它? 但是我使用以下内容在mongodb中定义架构
import { Schema } from 'mongoose';
export const TestSchema = new Schema(
{
test: String;
}
);