在编译 Nest.js 项目时出现了意外的类型错误,websockets 部分。

huangapple go评论61阅读模式
英文:

Unexpected type error in websockets when compiling nest js project

问题

这是我的代码

我尝试设置一个Web套接字网关如下。

events.gateways.ts

import {
  MessageBody,
  SubscribeMessage,
  WebSocketGateway,
  WebSocketServer,
} from "@nestjs/websockets";
import { Server } from "socket.io";

@WebSocketGateway({
  cors: {
    origin: "http://localhost:3000",
  },
})
export class EventsGateway {
  @WebSocketServer()
  server: Server;

  @SubscribeMessage("events")
  handleEvent(@MessageBody() data: string): string {
    return data;
  }
}

我将上述网关设置为模块的提供者。

events.module.ts

import { Module } from "@nestjs/common";
import { EventsGateway } from "./events.gateway";

@Module({
  providers: [EventsGateway],
})
export class EventsModule {}

最后,我将上述模块导入到app.module.ts中,现在我遇到了以下错误。

app.module.ts

import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { APP_FILTER } from "@nestjs/core";
import { AllExceptionFilter } from "./all-exception.filter";
import { ConfigModule } from "@nestjs/config";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Student } from "./student/entities/student.entity";
import { EventsModule } from "./events/events.module";

@Module({
  imports: [
    ConfigModule.forRoot(),
    TypeOrmModule.forRoot({
      type: "postgres",
      host: process.env.DB_HOST,
      port: parseInt(process.env.DB_PORT as string) || 5432,
      username: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_DATABASE,
      entities: [Student],
      synchronize: true,
    }),
    EventsModule,
  ],
  controllers: [AppController],
  providers: [
    {
      provide: APP_FILTER,
      useClass: AllExceptionFilter,
    },
    AppService,
  ],
})
export class AppModule {}

这是我一直遇到的错误。

TypeError: this.graphInspector.insertEntrypointDefinition is not a function
    at /home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/web-sockets-controller.js:108:33
    at Array.forEach (<anonymous>)
    at WebSocketsController.inspectEntrypointDefinitions (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/web-sockets-controller.js:106:25)
    at WebSocketsController.subscribeToServerEvents (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/web-sockets-controller.js:39:14)
    at WebSocketsController.connectGatewayToServer (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/web-sockets-controller.js:30:14)
    at SocketModule.connectGatewayToServer (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/socket-module.js:47:35)
    at /home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/socket-module.js:36:38
    at IteratorWithOperators.forEach (/home/Desktop/New Folder/nest_try_1/node_modules/iterare/lib/iterate.js:157:13)
    at SocketModule.connectAllGateways (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/socket-module.js:36:14)
    at /home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/socket-module.js:31:61

Node.js v19.4.0

我遵循文档,但一直遇到上述错误。

英文:

Below is my code

I tried to set a web socket gateway as below.

events.gateways.ts

import {
  MessageBody,
  SubscribeMessage,
  WebSocketGateway,
  WebSocketServer,
} from &quot;@nestjs/websockets&quot;;
import { Server } from &quot;socket.io&quot;;

@WebSocketGateway({
  cors: {
    origin: &quot;http://localhost:3000&quot;,
  },
})
export class EventsGateway {
  @WebSocketServer()
  server: Server;

  @SubscribeMessage(&quot;events&quot;)
  handleEvent(@MessageBody() data: string): string {
    return data;
  }
}

I set above gateway as a provider to a module.

events.module.ts

import { Module } from &quot;@nestjs/common&quot;;
import { EventsGateway } from &quot;./events.gateway&quot;;

@Module({
  providers: [EventsGateway],
})
export class EventsModule {}

Fianllt, i imported above module to the app.module.ts and now I am getting the error

app.module.ts

import { Module } from &quot;@nestjs/common&quot;;
import { AppController } from &quot;./app.controller&quot;;
import { AppService } from &quot;./app.service&quot;;
import { APP_FILTER } from &quot;@nestjs/core&quot;;
import { AllExceptionFilter } from &quot;./all-exception.filter&quot;;
import { ConfigModule } from &quot;@nestjs/config&quot;;
import { TypeOrmModule } from &quot;@nestjs/typeorm&quot;;
import { Student } from &quot;./student/entities/student.entity&quot;;
import { EventsModule } from &quot;./events/events.module&quot;;

@Module({
  imports: [
    ConfigModule.forRoot(),
    TypeOrmModule.forRoot({
      type: &quot;postgres&quot;,
      host: process.env.DB_HOST,
      port: parseInt(process.env.DB_PORT as string) | 5432,
      username: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_DATABASE,
      entities: [Student],
      synchronize: true,
    }),
    EventsModule,
  ],
  controllers: [AppController],
  providers: [
    {
      provide: APP_FILTER,
      useClass: AllExceptionFilter,
    },
    AppService,
  ],
})
export class AppModule {}

This is the error I am keep getting.

TypeError: this.graphInspector.insertEntrypointDefinition is not a function
    at /home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/web-sockets-controller.js:108:33
    at Array.forEach (&lt;anonymous&gt;)
    at WebSocketsController.inspectEntrypointDefinitions (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/web-sockets-controller.js:106:25)
    at WebSocketsController.subscribeToServerEvents (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/web-sockets-controller.js:39:14)
    at WebSocketsController.connectGatewayToServer (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/web-sockets-controller.js:30:14)
    at SocketModule.connectGatewayToServer (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/socket-module.js:47:35)
    at /home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/socket-module.js:36:38
    at IteratorWithOperators.forEach (/home/Desktop/New Folder/nest_try_1/node_modules/iterare/lib/iterate.js:157:13)
    at SocketModule.connectAllGateways (/home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/socket-module.js:36:14)
    at /home/Desktop/New Folder/nest_try_1/node_modules/@nestjs/websockets/socket-module.js:31:61

Node.js v19.4.0

I am follwing the documentation and I am keep getting above error.

答案1

得分: 0

你应该对齐你的依赖项,即,如果你正在使用 @nestjs/websockets v9.3.1,@nestjs/core 也必须是 v9.3.1。

运行 npx nest info 并查看。

这个错误是因为 @nestjs/websockets 在 v9.3 中添加了一个方法,该方法依赖于 @nestjs/core v9.3 中添加的一个方法。

顺便说一下,关于这个错误在 GitHub 上有已关闭的问题: https://github.com/nestjs/nest/issues?q=is%3Aissue+sort%3Aupdated-desc+is%3Aclosed+insertEntrypointDefinition

英文:

you should align your dependencies, ie., if you're using @nestjs/websockets v9.3.1, @nestjs/core must be in v9.3.1 as well

Run npx nest info and check that out.

That error is because @nestjs/websockets is invoking a method added in v9.3 which depends on a method added in v9.3 of @nestjs/core as well.

btw there are closed issues on GitHub about that error: https://github.com/nestjs/nest/issues?q=is%3Aissue+sort%3Aupdated-desc+is%3Aclosed+insertEntrypointDefinition

huangapple
  • 本文由 发表于 2023年2月6日 16:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定