【问题标题】:Property 'get' does not exist on type 'Cache'. NestJs project“缓存”类型上不存在属性“获取”。 NestJs 项目
【发布时间】:2022-01-23 11:00:57
【问题描述】:

我正在尝试通过关注此official guide 和此tutorial 为我的nestjs-graphql 项目设置Redis caching。我正在遵循提到的确切步骤,但我收到了Property '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';

@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'); //<--- This is what gives the error
    if(value){                                        // cacheManager doesn't have a get
      console.log({                                   // but both of the mentioned tutorials 
        data: value,                                  // use the same exact property
        loadsFrom: 'redis cache'
      })
    }
    return await this.usersService.findAll();
  }
}

【问题讨论】:

  • 尝试按照@ArekRGW 的建议添加缓存的导入。 import { Cache } from 'cache-manager';

标签: javascript node.js caching redis nestjs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 2022-06-22
    • 2016-09-07
    • 2021-09-08
    • 2018-01-07
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    相关资源
    最近更新 更多