No firebase.app.App instance is currently initialized

Viewed 2397

First, I wanted to be sure that all the firebase scripts are loaded and they are loaded in the proper order.

I solved this in a bit of a rudimentary way but it works :)

var my_firebase_init = function(){

    firebase.initializeApp(my_firebase_config);
    firebase.auth().languageCode = my_lang;

    console.log('FIREBASE INIT OK');


}

var firebase_version = 'https://www.gstatic.com/firebasejs/4.6.2/';

var scripts = [
    firebase_version+'firebase.js',
    firebase_version+'firebase-app.js',
    firebase_version+'firebase-auth.js',
    'my_firebase_init'

];

var script_loaded = [];

var deferred = new $.Deferred(),

pipe = deferred;

$.each(scripts , function(index, script) {
    pipe = pipe.pipe(function() {

        if(script == 'my_firebase_init'){
            if(script_loaded[0] && script_loaded[1] && script_loaded[2]){
                my_firebase_init();
            }
            return;
        }

        return $.getScript(script)
            .done(function( script, textStatus ) {
                 script_loaded[index] = true
            })
            .fail(function( jqxhr, settings, exception ) {
                 script_loaded[index] = false
        });                 
    });
});

deferred.resolve(); 

Now I can see console.log('FIREBASE INIT OK')

The next I try to use recaptcha like:

window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('id_container_recaptcha', {
    'size': 'invisible',
    'callback': function(response) {
        // reCAPTCHA solved, allow user to do action.
        // ...   
    },
    'expired-callback': function() {
         // Response expired. Ask user to solve reCAPTCHA again.
         // ...
    }

});

This is working good but from time to time I get this error:

No firebase.app.App instance is currently initialized

I noticed that if I wait a bit and try again ... works good!

So my question is : How I can check if firebase.app.App is initialized ?

2 Answers

you need to initialize firebase, before you can use it:

/* Firebase App is always required and must be first */
var firebase = require("firebase/app");
var app;

var firebaseConfig = {
    apiKey: "some-api-key",
    authDomain: "some-app.firebaseapp.com",
    databaseURL: "https://some-app.firebaseio.com",
    storageBucket: "some-app.appspot.com",
};

/* on DOM ready */
$(function() {

    app = firebase.initializeApp(firebaseConfig);

    /* check in here, if it had been loaded properly */
});

Import all from firebase;

import * firebase from 'firebase';

And initialize that after ngModule()

firebase.initializeApp(config);

Then all is same to your code

Related