nestjs @SubscribeMessage UnhandledPromiseRejectionWarning: TypeError: this.contextUtils.getContextFactory is not a function

Viewed 3374

I am seeing this error in my nestjs application

(node:16561) UnhandledPromiseRejectionWarning: TypeError: this.contextUtils.getContextFactory is not a function at WsContextCreator.getMetadata (/Users/sajankumarvijayan/Documents/projects/review.io/node_modules/@nestjs/websockets/context/ws-context-creator.js:73:50) at WsContextCreator.create (/Users/sajankumarvijayan/Documents/projects/review.io/node_modules/@nestjs/websockets/context/ws-context-creator.js:28:68)

Here is my example code:

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');
  }
}

If i remove the @SubscribeMessage annotation the error is gone. I think this exception is not handled but looking for some answers and help to fix this. Thank you.

5 Answers

I faced the same issue and it disappeared after I upgraded @nestjs/core and @nestjs/common to 7.x.x versions

It happened to me and in my case was a version issue, I was using version 6.x of packages @nestjs/common and @nestjs/core.

It got fixed after I made a major upgrade in those libraries. Since I didn't have any version restriction, I've performed a full major version upgrade. Following instructions for npm

One step

npm i -g npm-check-updates && ncu -u && npm i

Or breaking it down...

Install check updates in case you don't have it yet

npm install -g npm-check-updates

Gives you the list

ncu -u

Install

npm install

This will also update your package.json file

Faced the same error, updating dependency versions through the npm-check-updates helped

ncu -u

npm install

I figured the issue was the version I fixed by running yarn upgrade --scope @nestjs --latest does the magic.

I got the same error. To resolve it i updated nestjs with nest update -f -t latest, for some reason the -f flag was necessary for me, otherwise it wasn't updating.

Related