类型“Cache”上不存在属性“get”。NestJs项目

new*_*ser 9 javascript caching redis node.js nestjs

我正在尝试按照此官方指南和本教程来设置Redis caching我的nestjs项目。我正在按照提到的确切步骤进行操作,但出现此错误graphqlProperty 'get' does not exist on type 'Cache'.

这是确切的代码

import {User} from './user.entity'
import {Resolver, Query, ResolveField, Args, Parent, Mutation} from '@nestjs/graphql'
import { UsersService } from './users.service';
import { PostsService } from '../posts/posts.service';
import { CurrentUser } from 'src/auth/auth.resolver';
import { CACHE_MANAGER, Inject, UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from 'src/auth/graphql-auth.guard';
import { Cache } from 'cache-manager'; <---------this is you need to import
@Resolver(of => User)
export class UsersResolver {
  constructor(
    @Inject(CACHE_MANAGER) private cacheManager: Cache,
    private usersService: UsersService,
    private postsService: PostsService,
  ) {}

  @Query()
  async getUsers() {
    const value = await this.cacheManager.get('key'); //Now .get property is working.
    if(value){                                       
      console.log({                                  
        data: value,                                 
        loadsFrom: 'redis cache'
      })
    }
    return await this.usersService.findAll();
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 20

导入Cache使用的

@Inject(CACHE_MANAGER) private cacheManager: Cache

cache-manager

从“缓存管理器”导入{缓存};


小智 1

代码不多,但我想到的一件事是 Nest 无法解决依赖关系,或者错误地解决了它。我没有看到任何类型的导入,Cache看看您提供的文档(下面引用)也许会有帮助。

Cache 类是从缓存管理器导入的,而 CACHE_MANAGER 令牌是从 @nestjs/common 包导入的。