Javascript Firebase:没有Firebase应用程序';[默认]';已在使用保留的托管URL时创建
我已经按照指南设置了firebase web项目 我决定使用保留的托管URL,因为它似乎是处理项目的本地、登台和生产环境的一个很好的解决方案 问题是,在我的一个组件中尝试使用firebase时:Javascript Firebase:没有Firebase应用程序';[默认]';已在使用保留的托管URL时创建,javascript,reactjs,firebase,web,firebase-authentication,Javascript,Reactjs,Firebase,Web,Firebase Authentication,我已经按照指南设置了firebase web项目 我决定使用保留的托管URL,因为它似乎是处理项目的本地、登台和生产环境的一个很好的解决方案 问题是,在我的一个组件中尝试使用firebase时: firebase.auth() 我得到以下错误 尚未创建Firebase应用程序“[默认]”-请调用Firebase应用程序。initializeApp() index.html文件 <body> <noscript>You need to enable JavaScr
firebase.auth()
我得到以下错误
尚未创建Firebase应用程序“[默认]”-请调用Firebase应用程序。initializeApp()
index.html文件
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.9.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.9.3/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.9.3/firebase-analytics.js"></script>
<script src="/__/firebase/init.js"></script>
</body>
import firebase from "firebase";
class LoginModal extends React.Component {
sendVerificationCode() {
var phoneNumber = this.userPhoneNumber;
console.log("sending verification code to: ", phoneNumber);
var appVerifier = window.recaptchaVerifier;
firebase
.auth()
.signInWithPhoneNumber(phoneNumber, appVerifier)
.then(function(confirmationResult) {
// SMS sent. Prompt user to type the code from the message, then sign the
// user in with confirmationResult.confirm(code).
window.confirmationResult = confirmationResult;
console.log("confirmationResult = ", confirmationResult);
})
.catch(function(error) {
// Error; SMS not sent
console.log("error = ", error);
// ...
});
}
}
调用该函数(sendVerificationCode())时,应用程序崩溃,出现以下错误:
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
此外,我在控制台中获得此输出(可能相关,但不确定…):
init.js:1未捕获的语法错误:如果您尝试
window.firebase
您将实际看到firebase的实例已初始化 仅当您使用Firebase CLI为站点提供服务时,保留的托管URL才在本地提供:
firebase服务
你的网站应该是可见的@http://localhost:5000
另一种解决方案是确定项目是否已加载,如果未加载,则加载测试环境:
if(!firebase.apps.length){
firebase.initializeApp({
//firebase配置对象
})
}
window.firebase