Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

polyfills.js:3056 Unhandled Promise rejection: _xxx is not a function in Angular

My Angular 15 app.component.ts throws this error to the console:

polyfills.js:3056 Unhandled Promise rejection: _this.isAppActive is not a function ; Zone: <root> ; Task: null ; Value: TypeError: _this.isAppActive is not a function
    at main.js:152:21
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (vendor.js:216450:24)
    at _next (vendor.js:216469:9)
    at vendor.js:216474:7
    at new ZoneAwarePromise (polyfills.js:3412:21)
    at vendor.js:216466:12
    at Object.next (main.js:169:11)
    at ConsumerObserver.next (vendor.js:114130:25)
    at SafeSubscriber._next (vendor.js:114099:22) TypeError: _this.isAppActive is not a function
    at http://localhost/main.js:152:21
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (http://localhost/vendor.js:216450:24)
    at _next (http://localhost/vendor.js:216469:9)
    at http://localhost/vendor.js:216474:7
    at new ZoneAwarePromise (http://localhost/polyfills.js:3412:21)
    at http://localhost/vendor.js:216466:12
    at Object.next (http://localhost/main.js:169:11)
    at ConsumerObserver.next (http://localhost/vendor.js:114130:25)
    at SafeSubscriber._next (http://localhost/vendor.js:114099:22)

This error is raised on the line calling the isAppActive (promise) function after successfully receiving the Firebase token:

constructor(
    private platform: Platform, private fbNotifications: FbNotificationsService, private fbData: FbRtdbService,
    private route: Router, ...
  ) {
    this.fbNotifications.onFCMTokenChanged
      .pipe(takeUntil(this._unsubscribeAll))
      .subscribe({
        async next(token) { 
          if (token && !this.appLaunched) {
            this.firebaseToken = token;
            console.log('Token received in listener: ', token);
            if (token == 'web-platform') {
              this.stopApp = true;
              await this.closeLoader();
              this.taService.dialogClose("Unsupported Platform", "xxxxx is supported on mobile devices only at this time. Please install directly from the store.").then(() => {
                App.exitApp();
              });
            } else {
  
              this.appLaunched = true;
              
              this.isAppActive().then((isActive) => {       //<<<====== ERROR HERE
                console.log('Returned from isAppActive: ', isActive);
                if (isActive) {
                  this.initializeApp();
                } else if (isActive == 0) {
                  SplashScreen.hide();
                  this.taService.dialogClose("Thank You", "Thank you for your interest in using xxxx. At this time we have reached our quota for new members. Please open the app again in a few days as we open up for more members.").then(() => {
                    App.exitApp();
                  });
                }
              }).catch(function () {
                console.log("isAppActive Promise Rejected: ");
              });
            }
            this._unsubscribeAll.next(true);
            this._unsubscribeAll.complete();
          }
        },
        error(err) { 
          
        }
      });

The isAppActive function is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

isAppActive() {
    return new Promise((resolve, reject) => {
      this.awsdb.runQuery("getAppSettings", {}).subscribe({
        next(res) { 
          console.log('Received app settings: ', res);
          if (!res) {
            //DB request timed out - DB not available
            SplashScreen.hide();
            this.taService.dialogClose("Database Error", "Ooooops. The system Database is not available now. Please try again later").then((resDialog) => {
              if (resDialog) {
                App.exitApp();
              }
            });
          } else {
            resolve(res[0].s["~properties"].appActive);
          }   
        },
        error(err) { 
          console.log('TG Query Error: ' + err.description);
          //Route to some error page?
          SplashScreen.hide();
          this.taService.dialogClose("Network Error", "Ooooops. I can't find any Internet at this time");
          reject(-1);
        }
      });
    });
  }

I can see this is coming from polyfills, but the polyfills.ts file looks innocent:

import './zone-flags';
import 'zone.js';  // Included with Angular CLI.
(window as any)['global'] = window;
global.Buffer = global.Buffer || require('buffer').Buffer;

What am I missing, please?

>Solution :

Because you’re using a non-arrow function for next, this there is the enclosing object holding the next function that you’re giving to subscribe; not the object you’re constructing.

The next function should be an arrow function to access the object being created using this:

.subscribe({
    next: async (token) => {

Or do something like put const that = this; at the top of the constructor and use that so you aren’t relying on this that can change depending on the context.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading