Am completely new to nestjs and trying to inject a service into a gateway. Not able to get it to work. Following is the code,
#notification.gateway.ts
@WebSocketGateway()
export class NotificationsGateway implements OnGatewayConnection {
constructor(private notificationService: NotificationsService) {}
afterInit(server: any) {
this.notificationService.server = server;
}
}
#notification.service.ts
@Injectable()
export class NotificationsService {
public server: Server = null;
// Removing the inject model line makes the service available in the gateway
constructor(
@InjectModel(Notification.name)
private notificationModel: Model<NotificationDocument>
) {}
}
#notifications.module.ts
import { TenancyModule } from '../tenancy';
@Module({
imports: [
TenancyModule.forFeature([
{ name: Notification.name, schema: NotificationSchema },
]),
],
providers: [NotificationsService, NotificationsGateway]
})
export class NotificationsModule {}
Am getting the following error,
TypeError: Cannot set property 'server' of undefined
at NotificationsGateway.afterInit (c:\notifications\notifications.gateway.js:25:41)
Removing the InjectModel in the service constructor makes the service available, but I would the model to persist the notifications.
Any help on how to resolve this highly appreciated. Thanks.