Typescript 将服务注入网关-NestJS
我是nestjs的新手,正在尝试将服务注入网关。无法让它工作。代码如下: #通知.gateway.tsTypescript 将服务注入网关-NestJS,typescript,websocket,nestjs,nestjs-gateways,Typescript,Websocket,Nestjs,Nestjs Gateways,我是nestjs的新手,正在尝试将服务注入网关。无法让它工作。代码如下: #通知.gateway.ts @WebSocketGateway() export class NotificationsGateway implements OnGatewayConnection { constructor(private notificationService: NotificationsService) {} afterInit(server: any) {
@WebSocketGateway()
export class NotificationsGateway implements OnGatewayConnection {
constructor(private notificationService: NotificationsService) {}
afterInit(server: any) {
this.notificationService.server = server;
}
}
#通知.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>
) {}
}
我得到以下错误
TypeError: Cannot set property 'server' of undefined
at NotificationsGateway.afterInit (c:\notifications\notifications.gateway.js:25:41)
删除服务构造函数中的InjectModel可以使服务可用,但我希望使用该模型来持久化通知
非常感谢您对如何解决此问题的任何帮助。谢谢。您的模型请求是否有范围(即您的mongoose连接是否根据请求而改变)?@JayMcDoniel正在使用TenancModule。。。这会产生影响吗?它会改变每个请求的mongoose连接吗?
TypeError: Cannot set property 'server' of undefined
at NotificationsGateway.afterInit (c:\notifications\notifications.gateway.js:25:41)