Home > Software engineering >  React Native @microsoft/signalr showing Error : ReferenceError: document is not defined
React Native @microsoft/signalr showing Error : ReferenceError: document is not defined

Time:11-27

I am Creating Connection like this its working fine before but now its showing error.

Here is Error I got

hub failed ReferenceError: document is not defined
    at HubConnection._startWithStateTransitions$ (HubConnection.js:110)
    at tryCatch (runtime.js:63)
    at Generator.invoke [as _invoke] (runtime.js:294)
    at Generator.next (runtime.js:119)
    at tryCatch (runtime.js:63)

Here is my Code.

const createConnection = () => {
    _hubConnection = new HubConnectionBuilder().withUrl(SOCKET_HUB_URL, {
      skipNegotiation: true,
      transport: HttpTransportType.WebSockets,
    })
      .withAutomaticReconnect([0, 0, 0, 0, 0])
      .build();
    
    setTimeout(() => {
      _hubConnection.start()
      .then((data) => {
        console.log('hub started', data);
        refreshMessages();
      })
      .catch(err => {
        console.log('hub failed', err);
      });
    }, 5000);
  }

Thanks in advance

CodePudding user response:

You can workaround this by adding:

if (!globalThis.document) {
  (globalThis.document as any) = undefined;
}

to your app. See https://github.com/dotnet/aspnetcore/issues/38286#issuecomment-970580861 for more context.

CodePudding user response:

I am able to fix the issue by putting these two lines in start of my function

if (!globalThis.document) {
  (globalThis.document as any) = undefined;
}

like This

const createConnection = () => {
    if (!globalThis.document) {
      (globalThis.document) = undefined;
    }
    
    _hubConnection = new HubConnectionBuilder().withUrl(SOCKET_HUB_URL, {
      skipNegotiation: true,
      transport: HttpTransportType.WebSockets,
    })
      .withAutomaticReconnect([0, 0, 0, 0, 0])
      .build();
      _hubConnection.start()
      .then((data) => {
        console.log('hub started', data);
        refreshMessages();
      })
      .catch(err => {
        console.log('hub failed', err);
      });
  }

Its a workaround for now and hopefully fixed with Updated Version of @microsoft/signlr

for more details check following link https://github.com/dotnet/aspnetcore/issues/38286

  • Related