NativeScript / firebase

Modular Firebase 🔥 implementation for NativeScript. Supports both iOS & Android platforms for all Firebase services.
https://docs.nativescript.org/plugins/firebase-core.html
Apache License 2.0
56 stars 50 forks source link

firebase() doesn't return app instance #122

Closed R-chie closed 2 years ago

R-chie commented 2 years ago

"@nativescript/firebase-core": "^2.2.9",

When i try to initialize firebase app, firebase() call doesn't return app instance

@NgModule({
  bootstrap: [AppComponent],
  imports: [],
  declarations: [AppComponent],
  providers: [
    ...APP_INTERCEPTORS,
  ],
  schemas: [NO_ERRORS_SCHEMA]
})
export class AppModule {
  constructor(private _router: Router, private appService: AppService) {
    console.log('firebase -->?', firebase()); //  <<--  here {}
    firebase().initializeApp().then(_ => console.log('Firebase app initialized'));
  }
}

Can anyone explain what's wrong with that? Thanks

  "dependencies": {
    "@angular/animations": "~13.2.0",
    "@angular/common": "~13.2.0",
    "@angular/compiler": "~13.2.0",
    "@angular/core": "~13.2.0",
    "@angular/forms": "~13.2.0",
    "@angular/platform-browser": "~13.2.0",
    "@angular/platform-browser-dynamic": "~13.2.0",
    "@angular/router": "~13.2.0",
    "@kefah/nativescript-google-maps": "^1.0.7",
    "@nativescript-community/ui-material-bottomnavigationbar": "^7.0.25",
    "@nativescript-community/ui-material-bottomsheet": "^7.0.25",
    "@nativescript/angular": "^13.0.0",
    "@nativescript/appversion": "^2.0.0",
    "@nativescript/core": "~8.3.4",
    "@nativescript/firebase-core": "^2.2.9",
    "@nativescript/geolocation": "^8.0.2",
    "@nativescript/localize": "^5.0.4",
    "@nativescript/secure-storage": "^3.0.0",
    "@nativescript/theme": "~3.0.2",
    "@sergeymell/nativescript-svg": "^1.1.2",
    "rxjs": "~7.5.0",
    "zone.js": "~0.11.5"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~13.2.0",
    "@angular/compiler-cli": "~13.2.0",
    "@nativescript/android": "8.3.1",
    "@nativescript/types": "~8.3.1",
    "@nativescript/webpack": "~5.0.6",
    "@ngtools/webpack": "~13.2.0",
    "copy-webpack-plugin": "^11.0.0",
    "typescript": "~4.5.5"
  }

after watching problem deeper i got an exception after firebase().initializeApp() call

JS: ERROR Error: Uncaught (in promise): Error: Cannot read property 'initializeApp' of undefined
JS: Error: Cannot read property 'initializeApp' of undefined
JS:     at file: src\webpack:\szybkigroszyk\node_modules\@nativescript\firebase-core\index.android.js:283:0
triniwiz commented 2 years ago

Cleaning the project should fix you up ns clean

dafranklin commented 2 years ago

I have the same issue. ns clean didn't help. I try to init firebase in the app component with this.

import { firebase } from '@nativescript/firebase-core'; ... ngOnInit() { firebase().initializeApp().then( () => { console.log("firebase.init done!"); }, error => { console.log(firebase.init error: ${error}); } ); }

anyone had the same issue and solved it?

triniwiz commented 2 years ago

You should call init outside of your app module

gyan-nsf commented 2 years ago

same issue here. Anyone find the fix ? share please.

async function initFireBase() {
  try {
    const defaultApp = await firebase().initializeApp();
  } catch (err) {
    console.log(err);
  }
}

Console output


Successfully synced application org.nsf.mobileaudit on device RZ8N20KGVZE.
  JS: Angular is running in development mode. Call enableProdMode() to enable production mode.
  JS: ERROR Error: Uncaught (in promise): Error: Cannot read property 'initializeApp' of undefined
  JS: Error: Cannot read property 'initializeApp' of undefined
  JS:     at file: src/webpack:/NSFAudit/node_modules/@nativescript/firebase-core/index.android.js:328:0
  JS:     at new ZoneAwarePromise (file: src/webpack:/NSFAudit/node_modules/zone.js/fesm2015/zone.js:1429:0)
  JS:     at Firebase.initializeApp (file: src/webpack:/NSFAudit/node_modules/@nativescript/firebase-core/index.android.js:267:0)
  JS:     at AppComponent.initifireBase (file: src/webpack:/NSFAudit/src/app/app.component.ts:85:0)
  JS:     at AppComponent.ngOnInit (file: src/webpack:/NSFAudit/src/app/app.component.ts:73:0)
  JS:     at callHook (file: src/webpack:/NSFAudit/node_modules/@angular/core/fesm2015/core.mjs:2491:0)
  JS:     at callHooks (file: src/webpack:/NSFAudit/node_modules/@angular/core/fesm2015/core.mjs:2460:0)
  JS:     at executeInitAndCheckHooks (file: src/webpack:/NSFAudit/node_modules/@angular/core/fesm2015/core.mjs:2411:0)
  JS:     at refreshView (file: src/webpack:/NSFAudit/node_modules/@angular/core/fesm2015/core.mjs:11979:0)
  JS:     at renderComponentOrTemplate
gyan-nsf commented 2 years ago
Screenshot 2022-09-15 at 9 49 13 AM

Error is here Looks like FirebaseApp is not a property of com.google.firebase

gyan-nsf commented 2 years ago

any update on this please ?

triniwiz commented 2 years ago

Can you try adding one of the optional modules e.g database or storage ?

wibimaster commented 2 years ago

Hi,

Same error here ; project cleaned, some other optional modules installed... No error on logcat (android), but same object FirebaseApp empty on firebase-core node_modules

@gyan-nsf @R-chie Have you found a solution since?

Thanks !

aymenelhajayed commented 2 years ago

Hello,

I have the same issu firebase().initializeApp() is an Empty function, here is my package.json "dependencies": { "@angular/animations": "^12.2.5", "@angular/common": "^12.2.5", "@angular/compiler": "^12.2.5", "@angular/core": "^12.2.5", "@angular/forms": "^12.2.5", "@angular/platform-browser": "^12.2.5", "@angular/platform-browser-dynamic": "^12.2.5", "@angular/router": "^12.2.5", "@danvick/ngx-translate-nativescript-loader": "3.0.1", "@nativescript/angular": "^12.2.0", "@nativescript/core": "8.3.5", "@nativescript/email": "^2.0.5", "@nativescript/firebase": "^11.1.3", "@nativescript/secure-storage": "^3.0.0", "@nativescript/social-share": "^2.0.4", "@nativescript/theme": "~3.0.0", "@ngx-translate/core": "^13.0.0", "copyfiles": "^2.4.1", "nativescript-dna-deviceinfo": "^3.7.1", "nativescript-email": "^1.6.0", "nativescript-gtm": "^1.0.0", "nativescript-openurl": "^1.0.2", "nativescript-phone": "^3.0.1", "nativescript-social-share": "^1.6.0", "reflect-metadata": "~0.1.12", "rimraf": "^3.0.2", "rxjs": "~7.3.0", "zone.js": "~0.11.4" }

triniwiz commented 2 years ago

Anyone with only firebase-core installed will need to install one of the other firebase modules .eg storage.

brysem commented 2 years ago

Experiencing the same issue. firebase() returns an empty object instead of firebase. I have the following modules installed for firebase:

"@nativescript/firebase-core": "^2.3.4",
"@nativescript/firebase-crashlytics": "^2.3.4",
aymenelhajayed commented 2 years ago

Hello @brysem , you will need to install "@nativescript/firebase-installations": "2.3.4", this will fix your issue

triniwiz commented 2 years ago

A couple things to try

  1. Those getting the following error JS: ERROR Error: Uncaught (in promise): Error: Cannot read property 'initializeApp' of undefined you need to install one of the other firebase packages

  2. Those using any non vanilla flavor try calling the initializeApp in the app.(ts|js)/ main.ts early as possible

brysem commented 2 years ago

Hello @brysem , you will need to install "@nativescript/firebase-installations": "2.3.4", this will fix your issue

Thank you @aymenelhajayed. This solved my problem.

For anyone else running into the problem setting up crashlytics. I will leave the following below.

After installing the following three dependencies I have crashlytics working.

"@nativescript/firebase-core": "^2.3.4",
"@nativescript/firebase-crashlytics": "^2.3.4",
"@nativescript/firebase-installations": "^2.3.4",

I mounted firebase in main.ts with the following code.

import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-crashlytics';

const firebaseApp = await firebase().initializeApp();

I can now use the log feature of crashlytics on the different pages in my app. Example:

import { firebase } from '@nativescript/firebase-core';

firebase().crashlytics().log('Login@mounted');