admin 管理员组文章数量: 1132277
源于一个报错
iesException [Error]: Nest can't resolve dependencies of the CacheInterceptor (?, Reflector). Please make sure that the argument "CACHE_MANAGER" at index [0] is available in the AppModule context.
Potential solutions:
Is AppModule a valid NestJS module?
If "CACHE_MANAGER" is a provider, is it part of the current AppModule?
If "CACHE_MANAGER" is exported from a separate @Module, is that module imported within AppModule? @Module({ imports: [ /* the Module containing "CACHE_MANAGER" */ ] })
在配置高速缓存(@nestjs/cache-manager)的时候出现这个报错,我是这样配置的:
Cache.registerAsync({
useFactory: async () => {
return {
stores: [
new Keyv({
store: new CacheableMemory({ ttl: 3000, lruSize: 5000 }),
}),
createKeyv('redis://localhost:6379'),
],
};
},
}),
然后就出现了这个报错,查找了很多资料说是没有提供出去
providers: [
{
provide: CACHE_MANAGER,
useExisting: CACHE_MANAGER,
},
],
exports: [CACHE_MANAGER],
显式地提供出去了,但是还是会报错。
后来分析问题 Cache.registerAsync 这样导出导入属于动态异步导入,在加载的时候可能未将这个“CACHE_MANAGER” 提供出去
解决
providers: [{
provide: CACHE_MANAGER,
useFactory: async () => {
return CACHE_MANAGER;
},
}],
exports: [CACHE_MANAGER],
使用异步提供器直接返回这个常量即可
全部代码
import { Global, Module } from '@nestjs/common';
import { CacheModule as Cache, CACHE_MANAGER } from '@nestjs/cache-manager';
import { createKeyv } from '@keyv/redis';
import { Keyv } from 'keyv';
import { CacheableMemory } from 'cacheable';
@Module({
imports: [
Cache.registerAsync({
useFactory: async () => {
return {
stores: [
new Keyv({
store: new CacheableMemory({ ttl: 3000, lruSize: 5000 }),
}),
createKeyv('redis://localhost:6379'),
],
};
},
}),
// Cache.register({
// isGlobal: true,
// // store: redisStore,
// // ttl: 30 * 1000,
// host: 'localhost',
// port: 6379,
// // ttl: 3 * 1000,
// }),
],
providers: [{
provide: CACHE_MANAGER,
useFactory: async () => {
return CACHE_MANAGER;
},
}],
exports: [CACHE_MANAGER],
})
export class CacheModule { }
使用方全部代码
import {
HttpException,
Body,
Controller,
Get,
Post,
UploadedFile,
Head,
Param,
Query,
Headers,
Inject,
LoggerService,
Logger,
HttpStatus,
UseInterceptors,
ParseIntPipe,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
// import { AppService } from './app.service';
import { User } from './entity/User/User';
import { Repository } from 'typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ConfigEnum } from "./enum/config.enum"
// import { Logger } from 'winston';
// import { Logger } from "nestjs-pino"
import { InjectRedis } from '@nestjs-modules/ioredis';
import { CACHE_MANAGER, CacheInterceptor, Cache } from "@nestjs/cache-manager"
import { MailerService } from '@nestjs-modules/mailer';
import { FileInterceptor } from '@nestjs/platform-express';
import { PipeService } from './common/pipe/pipe.service';
@Controller()
@UseInterceptors(CacheInterceptor)
export class AppController {
// private readonly appService: AppService
constructor(
@Inject(CACHE_MANAGER)
private readonly cacheManager: Cache,
) { }
}
这样即可得到完整解决
本文标签: Valid NestJs AppModule Provider
版权声明:本文标题:关于nestjs异步提供器使用(Is AppModule a valid NestJS module? If “CACHE_MANAGER“ is a provider, is it part of) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.520sys.cn/xp/1765519713a1852030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论