ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Ionic v2 - Loading Service - Throwing error "cannot read property 'nativeElement' ..."

Open tonyawad88 opened this issue 10 years ago • 53 comments

Short description of the problem:

I have a welcome page that invokes the Loading service in its constructor. As soon as the page loads for the first time the error / exception is thrown.

What behavior are you expecting?

No exceptions thrown.

Steps to reproduce:

  1. Create a new ionic project ionic start MyIonic2Project tutorial --v2 --ts
  2. On the root page, add in its constructor the following:
import {Page,Loading,NavController} from 'ionic-angular';
@Page({
  templateUrl: 'build/pages/hello-ionic/hello-ionic.html'
})
export class HelloIonicPage {
  constructor(private nav: NavController) {
    this.nav =nav;
    let loading = Loading.create({
      content: "Please wait...",
      duration: 3000,
      dismissOnPageChange: true
    });
    this.nav.present(loading);
  }
}

3 . Check the console for the exception

Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc)

browser_adapter.ts:73 STACKTRACE:BrowserDomAdapter.logError @ browser_adapter.ts:73ExceptionHandler.call @ exception_handler.ts:52(anonymous function) @ application_ref.ts:258schedulerFn @ async.ts:127SafeSubscriber.__tryOrUnsub @ Subscriber.js:166SafeSubscriber.next @ Subscriber.js:115Subscriber._next @ Subscriber.js:74Subscriber.next @ Subscriber.js:51Subject._finalNext @ Subject.js:124Subject._next @ Subject.js:116Subject.next @ Subject.js:73EventEmitter.emit @ async.ts:113NgZone._zoneImpl.ng_zone_impl_1.NgZoneImpl.onError @ ng_zone.ts:134NgZoneImpl.inner.inner.fork.onHandleError @ ng_zone_impl.ts:88ZoneDelegate.handleError @ angular2-polyfills.js:336Zone.runGuarded @ angular2-polyfills.js:244NgZoneImpl.runInner @ ng_zone_impl.ts:97NgZone.run @ ng_zone.ts:204NavController._afterTrans @ nav-controller.js:1003(anonymous function) @ nav-controller.js:979Animation._didFinish @ animation.js:590Animation.play @ animation.js:264(anonymous function) @ nav-controller.js:990ZoneDelegate.invoke @ angular2-polyfills.js:332Zone.run @ angular2-polyfills.js:227NgZoneImpl.runOuter @ ng_zone_impl.ts:98NgZone.runOutsideAngular @ ng_zone.ts:218NavController._beforeTrans @ nav-controller.js:948NavController._postRender @ nav-controller.js:930(anonymous function) @ nav-controller.js:865(anonymous function) @ nav-controller.js:1217promiseReactionJob @ es6-shim.js:2171(anonymous function) @ es6-shim.js:2159Item.run @ browser.js:64drainQueue @ browser.js:34ZoneDelegate.invokeTask @ angular2-polyfills.js:365NgZoneImpl.inner.inner.fork.onInvokeTask @ ng_zone_impl.ts:54ZoneDelegate.invokeTask @ angular2-polyfills.js:364Zone.runTask @ angular2-polyfills.js:263ZoneTask.invoke @ angular2-polyfills.js:431
browser_adapter.ts:73 TypeError: Cannot read property 'nativeElement' of undefined
    at new LoadingMdPopOut (http://localhost:8101/build/js/app.bundle.js:44356:40)
    at Function.Transition.createTransition (http://localhost:8101/build/js/app.bundle.js:63476:16)
    at http://localhost:8101/build/js/app.bundle.js:46769:58
    at ZoneDelegate.invoke (http://localhost:8101/build/js/angular2-polyfills.js:332:29)
    at Zone.run (http://localhost:8101/build/js/angular2-polyfills.js:227:44)
    at NgZoneImpl.runOuter (http://localhost:8101/build/js/app.bundle.js:26898:71)
    at NgZone.runOutsideAngular (http://localhost:8101/build/js/app.bundle.js:26817:80)
    at Portal.NavController._beforeTrans (http://localhost:8101/build/js/app.bundle.js:46759:20)
    at Portal.NavController._postRender (http://localhost:8101/build/js/app.bundle.js:46741:14)
    at Portal.NavController._render (http://localhost:8101/build/js/app.bundle.js:46657:18)
  -------------   Elapsed: 77 ms; At: Sun Apr 17 2016 13:54:49 GMT-0400 (EDT)   -------------  
    at Object.Zone.longStackTraceZoneSpec.onScheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:1354:23)
    at ZoneDelegate.scheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:342:50)
    at Zone.scheduleMacroTask (http://localhost:8101/build/js/angular2-polyfills.js:283:40)
    at http://localhost:8101/build/js/angular2-polyfills.js:133:26
    at setTimeout (eval at createNamedFn (http://localhost:8101/build/js/angular2-polyfills.js:973:18), <anonymous>:3:37)
    at process.nextTick (http://localhost:8101/build/js/app.bundle.js:64854:9)
    at enqueuePromiseReactionJob (http://localhost:8101/build/js/app.bundle.js:37073:7)
    at fulfillPromise (http://localhost:8101/build/js/app.bundle.js:37099:9)
  -------------   Elapsed: 52 ms; At: Sun Apr 17 2016 13:54:49 GMT-0400 (EDT)   -------------  
    at Object.Zone.longStackTraceZoneSpec.onScheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:1354:23)
    at ZoneDelegate.scheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:342:50)
    at Zone.scheduleEventTask (http://localhost:8101/build/js/angular2-polyfills.js:286:40)
    at zoneAwareAddEventListener (http://localhost:8101/build/js/angular2-polyfills.js:880:11)
    at XMLHttpRequest.addEventListener (eval at createNamedFn (http://localhost:8101/build/js/angular2-polyfills.js:973:18), <anonymous>:3:43)
    at XMLHttpRequest.desc.set [as onload] (http://localhost:8101/build/js/angular2-polyfills.js:776:19)
    at XHRImpl.get (http://localhost:8101/build/js/app.bundle.js:30356:20)
    at TemplateNormalizer.normalizeTemplate (http://localhost:8101/build/js/app.bundle.js:10294:30)
  -------------   Elapsed: 327 ms; At: Sun Apr 17 2016 13:54:49 GMT-0400 (EDT)   -------------  
    at Object.Zone.longStackTraceZoneSpec.onScheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:1354:23)
    at ZoneDelegate.scheduleTask (http://localhost:8101/build/js/angular2-polyfills.js:342:50)
    at Zone.scheduleMacroTask (http://localhost:8101/build/js/angular2-polyfills.js:283:40)
    at http://localhost:8101/build/js/angular2-polyfills.js:133:26
    at setTimeout (eval at createNamedFn (http://localhost:8101/build/js/angular2-polyfills.js:973:18), <anonymous>:3:37)
    at process.nextTick (http://localhost:8101/build/js/app.bundle.js:64854:9)
    at enqueuePromiseReactionJob (http://localhost:8101/build/js/app.bundle.js:37073:7)
    at Promise.then (http://localhost:8101/build/js/app.bundle.js:37438:11)

Which Ionic Version? 2.x

Plunker that shows an example of your issue

http://plnkr.co/edit/fA0KB70ggJd0nptNvYpl?p=preview

Run ionic info from terminal/cmd prompt: (paste output below)

WARN: ionic.config.js has been deprecated, you can remove it.

Your system information:

Cordova CLI: 6.0.0
Gulp version:  CLI version 3.9.0
Gulp local:   Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.4
Ionic CLI Version: 2.0.0-beta.25
Ionic App Lib Version: 2.0.0-beta.15
ios-deploy version: 1.8.2 
ios-sim version: 5.0.3 
OS: Mac OS X El Capitan
Node Version: v0.12.7
Xcode version: Xcode 7.3 Build version 7D175 

tonyawad88 avatar Apr 17 '16 18:04 tonyawad88

Hello @tonyawad88,

are you using this Loading service, to load resources before your app starts rendering? I guess I stumbled uppon the same requirement and opened a more general question/request. Maybe you have a look and could link these issues, if they are similar to you?

https://github.com/driftyco/ionic/issues/6148

ataraxus avatar Apr 18 '16 07:04 ataraxus

@ataraxus Hii, I don't think they are related. I am able to create a barebone ionic 2 plunker and replicate the issue I am having with my app.

tonyawad88 avatar Apr 18 '16 10:04 tonyawad88

I have the same issue, though only when including the dismissOnPageChange: true parameter. (Does not throw an error if it's set to false)

XertroV avatar Apr 20 '16 03:04 XertroV

I encounter the same error, it seems that it happens when I try to dismiss the loading dialog "too quickly", e.g. when I get data out a cache instead of doing a HTTP request.

HereThereBeMonsters avatar Apr 20 '16 15:04 HereThereBeMonsters

Thanks for the info everyone! I am able to reproduce this and we are now looking further into this.

jgw96 avatar Apr 20 '16 16:04 jgw96

Here is a workaround that I was able to get to work:

http://plnkr.co/edit/OyGY7VVNSSAjdstr54wG?p=preview

keithdmoore avatar May 06 '16 01:05 keithdmoore

I am having the same issue when trying to use the loading component at a global level. If anyone else is having trouble with that, you might consider this until this bug is resolved at least: http://www.joshmorony.com/how-to-create-a-custom-loading-component-in-ionic-2/

keithdmoore avatar May 06 '16 02:05 keithdmoore

Here is a plunker that is using loading in a global way. http://plnkr.co/edit/3TfK76XpqsnUvW3dQuG3?p=preview

I had to do some trickier things in my implementation because I am using EventEmitters to indicate when an http request is started and when it has completed. Im still having a few issues with it but if I wrap the loading.dismiss() in a setTimeout, it seems to work.

keithdmoore avatar May 06 '16 02:05 keithdmoore

@jgw96 Hi Justin, do you have any workaround to dismiss the loading popup manually from javascript ? Calling loading.dismiss() is always throwing the 'nativeelement' error. Thanks

tonyawad88 avatar May 20 '16 14:05 tonyawad88

Hey all! Can you all try with this plunker? Looks like this issue is not reproducible anymore in our latest beta.7 release! http://plnkr.co/edit/IIfn2RKAMGheD8ISVr9A?p=preview I am going to close this for now, as it seems it was fixed. But if anyone can reproduce it again with beta.7 then i will reopen. Thanks for using Ionic everyone!

jgw96 avatar May 20 '16 16:05 jgw96

@jgw96 I can still reproduce in beta 7. Enable the following property on Loading, and it will throw the 'native element' exception.

dismissOnPageChange: true

Any idea how to dismiss the loading from JS ? The use case in the plunker, where we use setTimeout are not too realistic. :)

I wonder if there is any event that we can emit to dismiss the "Loading" ?

Thank you !

tonyawad88 avatar May 20 '16 16:05 tonyawad88

@jgw96 Seems like a race condition or a digest type issue. When adding the dismissOnPageChange: true I was able to reproduce. My current hack to get around this is to do the below when displaying the loading component. I wouldn't classify this issue as fixed.

   setTimeout(()=>{
      this.nav.present(loading);
   });

keithdmoore avatar May 20 '16 18:05 keithdmoore

@tonyawad88 I am using EventEmitter to handle the dismissing of the loading component.

keithdmoore avatar May 20 '16 18:05 keithdmoore

Hello all, you all are correct, setting dismissOnPageChange to true makes this issue still reproducible, sorry about that! @tonyawad88 as a workaround you can use the dismiss() method on the loading component. So it would just be {{name of your loader}}.dismiss(). Hope that makes sense! I will look into this more.

jgw96 avatar May 20 '16 18:05 jgw96

@tonyawad88 to be more specific here is what I am doing. (currently on beta6) In a service:

  httpRequestEventEmitter: EventEmitter<string>;
  httpResponseEventEmitter: EventEmitter<string>;

  private sendHttpRequest(request: Request, event: string): Observable<any>  {
    return Observable.of(request)
      .do((req: Request) => { this.httpRequestEventEmitter.emit(event + '-request-started'); })
      .flatMap((req: Request) => { return this.http.request(req); })
      .finally(() => {
        this.httpResponseEventEmitter.emit(event + '-request-finished');
      });
  }

In app.ts:

    this.service.httpRequestEventEmitter.subscribe(
      (data: string) => { 
          this.loading = Loading.create({
             dismissOnPageChange: true
          });
          this.app.getComponent(ROOT_NAV).present(this.loading);
      }
    );
    this.service.httpResponseEventEmitter.subscribe(
      (data: string) => { 
          this.loading.dismiss();
      }
    );

keithdmoore avatar May 20 '16 18:05 keithdmoore

@keithdmoore Just to let you know we deprecated app.getComponent in beta.7 in favor of the native Angular 2 ViewChild decorator https://angular.io/docs/ts/latest/api/core/ViewChild-var.html .

jgw96 avatar May 20 '16 18:05 jgw96

@jgw96 Ok. Thanks. Seems like I did see a use of ViewChild in the ionic-conference app. Thanks for the heads up! I am about to upgrade right now.

keithdmoore avatar May 20 '16 18:05 keithdmoore

No problem! Let me know how the beta.7 update goes for you! Here are some awesome instructions to help you with the upgrade: https://github.com/driftyco/ionic/blob/2.0/CHANGELOG.md#angular-update-to-200-rc1

jgw96 avatar May 20 '16 18:05 jgw96

@keithdmoore @jgw96 this.loading.dismiss(); also throws the error.

It sure looks like a race. For now adding a settimeout of 500ms, around creating the "loading" prevents it from firing the exception.

Check this plunker out, if you display / hide the loading using the buttons, no exceptions occurs. As soon as you use it inside "onPageDidEnter", it throws the exception. http://plnkr.co/edit/GXugmXJi2qwaYQWKscqC

Thank you !

tonyawad88 avatar May 20 '16 22:05 tonyawad88

You might try using: ngAfterViewInit I had trouble with the onPageDidEnter

keithdmoore avatar May 20 '16 22:05 keithdmoore