nestjs @SubscribeMessage UnhandledPromiseRejectionWarning: TypeError: this.contextUtils.getContextFactory 不是函数

saj*_*jan 5 websocket node.js nestjs

我在我的 nestjs 应用程序中看到这个错误

(node:16561) UnhandledPromiseRejectionWarning: TypeError: this.contextUtils.getContextFactory 不是 WsContextCreator.getMetadata 的函数 (/Users/sajankumarvijayan/Documents/projects/review.io/node_modules/@nestjs/websockets/context/ws-context-creator .js:73:50) 在 WsContextCreator.create (/Users/sajankumarvijayan/Documents/projects/review.io/node_modules/@nestjs/websockets/context/ws-context-creator.js:28:68)

这是我的示例代码:

import {
   SubscribeMessage,
   WebSocketGateway,
   WebSocketServer,
} from '@nestjs/websockets';
import { Server } from 'ws';

@WebSocketGateway()
export class SocketGateway {
  @WebSocketServer()
  server: Server;

  @SubscribeMessage('event')
  onEvent(client: any, data: any): void {
    this.server.emit('update');
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我删除 @SubscribeMessage 注释,错误就会消失。我认为这个异常没有被处理,而是在寻找一些答案并帮助解决这个问题。谢谢你。

Žel*_*vić 11

我面临着同样的问题,它消失后,我升级@nestjs/core@nestjs/common7.x.x版本


Joe*_*oel 5

它发生在我身上,在我的情况下是版本问题,我使用的是@nestjs/common@nestjs/core包的 6.x 版。

在我对这些库进行重大升级后,它得到了修复。由于我没有任何版本限制,因此我执行了完整的主要版本升级。遵循npm 的说明

一步

npm i -g npm-check-updates && ncu -u && npm i
Run Code Online (Sandbox Code Playgroud)

或者拆开...

如果您还没有安装检查更新

npm install -g npm-check-updates
Run Code Online (Sandbox Code Playgroud)

给你名单

ncu -u
Run Code Online (Sandbox Code Playgroud)

安装

npm install
Run Code Online (Sandbox Code Playgroud)

这也将更新您的package.json文件


小智 2

遇到同样的错误,通过npm-check-updates帮助更新依赖版本

ncu -u

npm install