最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - firebase.auth is not a function - with multiple firebase app initialization - Stack Overflow

programmeradmin0浏览0评论

I am getting the error 'Uncaught TypeError: firebase.auth is not a function'.

My code was working perfectly, and then I implemented my clients api, and discovered that their api already uses and has initialized a different firebase app. So, as I couldn't initialise my firebase app in the usual way (ie. firebase.initializeApp(config) ), I followed the documentation on initializing multiple firebase apps as follows. (This results in the error)

var configOther = {
apiKey: "xxxx",
authDomain: "xxxx",
databaseURL: "xxxx",
projectId: "xxxx",
storageBucket: "xxxx",
messagingSenderId: "xxxx"
};

var firebaseOther = firebase.initializeApp(configOther, "firebaseOther");

console.log("auth: ", firebase.auth(firebaseOther)); //"Uncaught TypeError: firebase.auth is not a function"

Any ideas about what I'm doing wrong here? Many thanks.

Note - I have also tried the following shorthand notation:

firebaseOther.auth() 

instead of

firebase.auth(firebaseOther)

but this results in the same error.

I am getting the error 'Uncaught TypeError: firebase.auth is not a function'.

My code was working perfectly, and then I implemented my clients api, and discovered that their api already uses and has initialized a different firebase app. So, as I couldn't initialise my firebase app in the usual way (ie. firebase.initializeApp(config) ), I followed the documentation on initializing multiple firebase apps as follows. (This results in the error)

var configOther = {
apiKey: "xxxx",
authDomain: "xxxx",
databaseURL: "xxxx",
projectId: "xxxx",
storageBucket: "xxxx",
messagingSenderId: "xxxx"
};

var firebaseOther = firebase.initializeApp(configOther, "firebaseOther");

console.log("auth: ", firebase.auth(firebaseOther)); //"Uncaught TypeError: firebase.auth is not a function"

Any ideas about what I'm doing wrong here? Many thanks.

Note - I have also tried the following shorthand notation:

firebaseOther.auth() 

instead of

firebase.auth(firebaseOther)

but this results in the same error.

Share Improve this question edited Mar 25, 2019 at 15:24 KENdi 7,7512 gold badges17 silver badges31 bronze badges asked Mar 24, 2019 at 23:15 RicRic 651 gold badge1 silver badge8 bronze badges 8
  • 1 Have you included the appropriate Firebase script / module for auth? – Phil Commented Mar 24, 2019 at 23:17
  • See firebase.google.com/docs/web/setup#add_firebase_to_your_app – Phil Commented Mar 24, 2019 at 23:22
  • Currently I'm just including all of the firebase services using: <script src="gstatic.com/firebasejs/5.9.1/firebase.js"></script> so I think it should be there. Also, this was working before I implemented my clients API. I wonder if somewhere in their api, they are somehow overriding this - is that even possible? – Ric Commented Mar 24, 2019 at 23:39
  • I'd be checking for any other <script> tags using the cut-down firebase-app.js file as that will overwrite the firebase variable. Also check for any use of firebase = ... – Phil Commented Mar 24, 2019 at 23:56
  • 1 Turns out there was, and that's what I've done. I've now managed to get both firebase apps initialized and behaving themselves - thanks for your help. – Ric Commented Mar 25, 2019 at 2:42
 |  Show 3 more comments

3 Answers 3

Reset to default 11

You likely skipped the step for including the firebase-auth script in your page, as described by the documentation:

<!-- Firebase App is always required and must be first -->
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-app.js"></script>

<!-- Add additional services that you want to use -->
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-auth.js"></script>

The only explanation is that somewhere in the page, there is another <script> tag referencing the firebase-app.js file, eg

<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-app.js"></script>

This tag will be appearing after your inclusion of firebase.js or firebase-app.js and firebase-auth.js.

What happens is the inclusion of firebase-app.js sets the value of the global firebase variable, overriding anything that was previously set.

The solution is to either remove the duplicate Firebase script inclusions or at the very least, make sure the ones you want active are included last.

I had a similar problem which was solved by removing defer in the HTML that was given by the documentation:

        <script src="https://www.gstatic.com/firebasejs/7.17.2/firebase-app.js"></script>
        <script defer src="https://www.gstatic.com/firebasejs/7.17.2/firebase-auth.js"></script>
        <script defer src="https://www.gstatic.com/firebasejs/7.17.2/firebase-firestore.js"></script>

发布评论

评论列表(0)

  1. 暂无评论