标签: nestjs

Nest.js 无法解析依赖项,找不到我的错误

我正在做一个 Nest.js 程序,但我找不到我的依赖问题。我搜索了很多,找到了很多关于这个问题的答案,但我不明白为什么我的代码不能\xc2\xb4t 工作。所以我有一个产品模块,其中有他的 DTO\xc2\xb4s、实体、控制器、服务和模块,此外它还有一个服务接口。

\n

产品控制器

\n
import { Controller, Get } from '@nestjs/common';\nimport { ProductServiceInterface } from './interface/product.service.interface'\n\n@Controller('products')\nexport class ProductController {\n  constructor(private readonly productService: ProductServiceInterface) {}\n\n  @Get()\n  getHello(): string {\n    return this.productService.test();\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

产品服务接口

\n
import { Injectable } from '@nestjs/common';\nimport {\n  CreateProductInput,\n  CreateProductOutput,\n} from '../dto/create-product.dto';\nimport { FindProductOutput } from '../dto/find-product.dto';\n\nexport interface ProductServiceInterface {\n  create(input: CreateProductInput): Promise<CreateProductOutput>;\n  findProduct(productId: string): Promise<FindProductOutput>;\n  test();\n}\n
Run Code Online (Sandbox Code Playgroud)\n

产品服务

\n
import { Injectable } from '@nestjs/common';\nimport { ProductServiceInterface } from './interface/product.service.interface';\nimport {\n  CreateProductInput,\n …
Run Code Online (Sandbox Code Playgroud)

javascript node.js typeorm nestjs

2
推荐指数
1
解决办法
1146
查看次数

索引 [1] 处的模块属于“未定义”类型。检查您的导入语句和模块的类型

我在 NestJs 中遇到了循环依赖问题。错误:

Scope [AppModule -> PlanModule -> TaskModule] +17ms
Error: Nest cannot create the UserModule instance.
The module at index [1] of the UserModule "imports" array is undefined.
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

  • 在构造函数中的用户和任务文件中的解析器文件中使用forwardRef :
@Inject(forwardRef(() => TaskService))
private readonly tasksService: TaskService
Run Code Online (Sandbox Code Playgroud)
  • 实现OnModuleInit接口:
onModuleInit() {
  this.userService = this.moduleRef.get(UserService);
}
Run Code Online (Sandbox Code Playgroud)
  • 使用不带index.ts文件的导入:
import { TaskModule } from '../task/task.module';
Run Code Online (Sandbox Code Playgroud)

用户模块

@Module({
  providers: [UserService, UserResolver],
  exports: [UserService],
  imports: [JournalModule, PlanModule, TaskModule],
})
export class UserModule {}
Run Code Online (Sandbox Code Playgroud)

任务模块

@Module({
  providers: [TaskService, TaskResolver],
  exports: …
Run Code Online (Sandbox Code Playgroud)

circular-dependency typescript graphql nestjs

2
推荐指数
1
解决办法
3990
查看次数

如何通过缓存管理器检索存储在内存缓存中的所有数据

我使用缓存管理器作为内存中数据存储,如文档https://docs.nestjs.com/techniques/caching https://www.npmjs.com/package/cache-manager中所述

有没有可能的方法来检索所有数据作为示例

const value = this.cacheManager.getall();
Run Code Online (Sandbox Code Playgroud)

node.js nestjs

2
推荐指数
1
解决办法
4410
查看次数

运行 NestJS 调度程序而不启动 HTTP 服务器

我正在尝试为 NestJS 创建一个“worker”,它基本上聚合来自多个数据源的数据。由于我将此工作程序部署到 Kubernetes 集群中,因此我不需要启动 NestJS 内部 HTTP 服务器,但是,调度程序不会在没有app.listen.

主要.ts:

async function bootstrap() {
  const app = await NestFactory.create(WorkerModule);
  await app.listen(3030); // <-- I would like to delete this
}

bootstrap();
Run Code Online (Sandbox Code Playgroud)

工作模块.ts

@Module({
  imports: [
    ScheduleModule.forRoot(),
  ],
  providers: [
    // Scrappers
    DataScrapper,
  ],
})
export class WorkerModule {}
Run Code Online (Sandbox Code Playgroud)

数据抓取器.ts

@Injectable()
export class DataScrapper {
  @Interval(1000)
  async sync() {
    console.log('fetching...');
  }
}
Run Code Online (Sandbox Code Playgroud)

scheduler typescript nestjs

2
推荐指数
1
解决办法
1638
查看次数

如何在自定义装饰器中使用 NestJS Reflector?

我正在使用 a@SetMetaData('version', 'v2')为控制器中的 http 方法设置版本控制。然后我有一个自定义@Get()装饰器将版本作为后缀添加到控制器路由中。

/api/cats/v2/firstfive这样,当我有时,我就可以使用

@SetMetaData('version', 'v2')
@Get('firstfive')
Run Code Online (Sandbox Code Playgroud)

但我没有看到将 Reflector 注入到我的自定义 @Get 装饰器中的明确方法。

我的 Get 装饰器如下,

import { Get as _Get } from '@nestjs/common';
export function Get(path?: string) {
  version = /*this.reflector.get('version') or something similar */
  return applyDecorators(_Get(version+path));
}
Run Code Online (Sandbox Code Playgroud)

请帮帮我!谢谢!

javascript node.js nestjs

2
推荐指数
1
解决办法
1万
查看次数

React WebApp 调用 NestJS 后端中使用 PassportStrategy 的 Google 登录无法正常工作

我已经在 NestJS 后端中使用 PassportStrategy 实现了 Google 登录(NestJS 后端开发基于本指南: https: //medium.com/@nielsmeima/auth-in-nest-js-and-angular-463525b6e071)。

@Get('google')
@UseGuards(AuthGuard('google'))
googleLogin()
{
    // initiates the Google OAuth2 login flow
}

@Get('google/callback')
@UseGuards(AuthGuard('google'))
googleLoginCallback(@Req() req, @Res() res)
{
    // handles the Google OAuth2 callback
    const jwt: string = req.user.jwt;
    if (jwt)
        return res.status(HttpStatus.OK).cookie('jwt', jwt, {
            httpOnly: true
        }).redirect('/');
    else 
        res.redirect('http://localhost:4200/login/failure');
}
Run Code Online (Sandbox Code Playgroud)

使用 @UseGuards(AuthGuard('google')) 应该启动 google 登录:

super({
        clientID    : 'XXX',     // <- Replace this with your client id
        clientSecret: 'XXX', // <- Replace this with your client secret …
Run Code Online (Sandbox Code Playgroud)

reactjs passport.js google-signin nestjs

2
推荐指数
1
解决办法
5190
查看次数

为什么 Nestjs 在其实现中使用了如此多的类?

所以我刚刚开始从Express切换到使用NestJS

Express中,一切都是对象,无论是顶层应用程序路由器请求响应等。

NestJS中的一切都几乎相同。它融合了Express所拥有的一切以及更多功能。

有一点我不明白为什么Nest中的一切都如此基于类?

为所有东西定义类有什么意义?比如控制器模块(路由器)、中间件中间件的使用者等等。

我非常有兴趣了解确切的哲学和设计模式。

javascript api node.js express nestjs

2
推荐指数
1
解决办法
1914
查看次数

用于邮递员的 NestJs Swagger

有没有办法将生成的 swagger.json 作为集合导入到 Postman 中?

我尝试导入它,但端点未显示在 Postman 中?

我正在使用 NestJs 和 Swagger + Postman

swagger postman nestjs nestjs-swagger

2
推荐指数
1
解决办法
5548
查看次数

Nestjs如何同时使用http请求和Websocket

我在 Nestjs 中有一个控制器来处理 HTTP 请求。有一个 IoT 设备与服务器进行通信,并通过 post 请求将最新更改发送到服务器。同时,有一个移动应用程序应该使用 Websockets 实时接收更新。

我知道 HTTP 请求和 Websockets 在 Nestjs 中是不同的概念,但是有没有办法在我收到 HTTP 请求时发出事件?

任何想法将不胜感激。

node.js socket.io nestjs nestjs-gateways nestjs-socket.io

2
推荐指数
1
解决办法
3227
查看次数

Nestjs Graphql 突变/查询输入 dto 为空

我不知道我错过了什么,但当我调试时,突变或查询的输入始终为空,似乎输入是空对象

这是我的代码:

解析器

  @Query(() => String)
  async testMutation(@Args('args') args: UpvotePostInput) {
    return args.postId;
  }
Run Code Online (Sandbox Code Playgroud)

数据传输工具

import { InputType, Field } from '@nestjs/graphql';

@InputType()
export class UpvotePostInput {
  @Field(() => String)
  postId: string;
}
Run Code Online (Sandbox Code Playgroud)

空物体 在此输入图像描述

错误

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Query.testMutation.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "testMutation"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Query.testMutation.",
            "    at completeValue (/....js:559:13)", …
Run Code Online (Sandbox Code Playgroud)

graphql nestjs

2
推荐指数
2
解决办法
2672
查看次数