AhsanAyaz / ngx-device-detector

An Angular v7+ library to detect the device, OS, and browser details.
https://ahsanayaz.github.io/ngx-device-detector
MIT License
518 stars 101 forks source link

Angular Universal Error: window is not defined #182

Closed AbraamNader closed 3 years ago

AbraamNader commented 3 years ago

Angular Universal Error: window is not defined at device-detector.service.ts

Line number: 113

this.orientation = window.matchMedia('(orientation: landscape)').matches
      ? OrientationType.Landscape
      : OrientationType.Portrait;

Can you please add an undefined check before accessing the window object (e.g. typeof window !== 'undefined')? if it is, set a default value for orientation.

Note: this error occurs on the server despite using ngx device detector on the browser only.

Thank you for this awesome library.

Parsa-Sedigh commented 3 years ago

I get the same error. Angular: 10.1.0 "ngx-device-detector": "^2.0.1" I created the universal-device-detector.service and provided it in app.server.module the same as docs but I'm using that service in my component.

pritamgb commented 3 years ago

Facing the same issue. Any solution or workaround for this?

AhsanAyaz commented 3 years ago

@AbraamNader , @Parsa-Sedigh Closed by https://github.com/KoderLabs/ngx-device-detector/pull/183

Thanks to @pritamgb for the PR. New version 2.0.2 released with the fix

vroussel35 commented 3 years ago

Thanks - @AhsanAyaz , @pritamgb

On my side, there is a new error : ERROR TypeError: window.matchMedia is not a function (before, it was window is not defined)

I think the fix to test the window object should be the same than you used in the code : if (isPlatformBrowser(this.platformId) && typeof window !== 'undefined')

Unfortunately, I don't know how to the PR stuff by myself and so on :-)

In the meantime, will downgrade to 2.0.0 :)

Thanks you guys.

AhsanAyaz commented 3 years ago

@vroussel35 a new version 2.0.3 has been released with the fix. If you still find it as an issue, please comment and reopen the ticket :)