我对nestjs 完全陌生,并试图将服务注入网关。无法让它工作。以下是代码,
#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 {}
我收到以下错误,
TypeError: Cannot set property 'server' of undefined
at NotificationsGateway.afterInit (c:\notifications\notifications.gateway.js:25:41)
在服务构造函数中删除 InjectModel 使服务可用,但我希望模型保留通知。
任何有关如何解决此问题的帮助都非常感谢。谢谢。