Search code examples
angularfirebasefirebase-realtime-databaseangularfire2

How to use two firebase database in angular application


I have a environment variable like this

 export const environment = {   
production: false,
   firebaseConfig: {
        apiKey: '',
        authDomain: '',
        databaseURL: '',
        projectId: '',
        storageBucket: '',
        messagingSenderId: '',
        appId: '1:1702713223'   }, 
  firebase: {
        apiKey: '',
        authDomain: '',
        databaseURL: '',
        projectId: '',
        storageBucket: '',
        messagingSenderId: '',
        appId: '1:37840'   } };

I need to use environment.firebaseconfig in one component and environment.firebase in another component.

I am using angularfire for firebase connection, i have tried importing at the module level for initialization, but by defalut only one environment variable is set.

Any solution would be appreciated


Solution

  • Please have a look into this solution

    // ... do all required imports
    import {
        firebaseConfigA, firebaseAppNameA,
        firebaseConfigB, firebaseAppNameB
    } from "@app/env";
    import { FirebaseService } from "../../services/firebase.service";
    
    @Component({
        selector       : "page-home",
        templateUrl    : "home.html"
    })
    export class ScannerDemoPage implements OnInit {
        constructor(private _firebaseService: FirebaseService) { }
    
        ngOnInit() {
            // Initialize 1st application
            this._firebaseService.initFirebaseApp(firebaseConfigA, firebaseAppNameA);
            let myList = this._firebaseService.getList("/path1");
    
            // Initialize 2nd application
            this._firebaseService.initFirebaseApp(firebaseConfigB, firebaseAppNameB);
            let myObj = this._firebaseService.getObject("/path2");
        }
    }