目前尚未初始化firebase.app.App实例

时间:2017-11-26 00:38:24

标签: javascript firebase

首先,我想确保加载所有firebase脚本并按正确的顺序加载它们。

我以一种基本的方式解决了这个问题,但它有效:)

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(); 

现在我可以看到console.log('FIREBASE INIT OK')

接下来我尝试使用recaptcha,如:

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.
         // ...
    }

});

这很好用但我不时会收到这个错误:

  

目前尚未初始化firebase.app.App实例

我注意到如果我稍等一下再试一次......效果很好!

所以我的问题是: 如何检查firebase.app.App是否已初始化?

2 个答案:

答案 0 :(得分:0)

您需要先初始化firebase,然后才能使用它:

/* 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 */
});

答案 1 :(得分:0)

全部从firebase导入;

import * firebase from 'firebase';

并在ngModule()之后初始化它

firebase.initializeApp(config);

然后所有内容都与您的代码相同