gpt4 book ai didi

javascript - 如何在多个 HTML 页面中使用相同的 firebase 数据库?

转载 作者:行者123 更新时间:2023-12-02 23:41:50 25 4
gpt4 key购买 nike

我正在创建一个具有仪表板的应用程序,该仪表板链接到不同的 HTML 页面,这些页面执行不同的操作,需要更新我的 Firebase 数据库,使用单页应用程序我可以毫无问题地执行此操作,当我尝试执行此操作时会出现问题用多个页面来做

当我尝试从dashboard_2.html连接到数据库时,它说该应用程序已经存在(因为我已经从database.js的index.html调用它),如果我从js文件中删除配置部分并且只是让修改部分显示尚未创建 Firebase 应用程序。

index.html

    <script src="https://www.gstatic.com/firebasejs/ui/3.6.1/firebase-ui-auth__es.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/3.6.1/firebase-ui-auth.css" />
<!-- The core Firebase JS SDK is always required and must be listed first -->
<!-- Add additional services that you want to use -->
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-messaging.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-functions.js"></script>



<!-- Firebase -->
<script src="js/database.js"></script>

</head>

<body>
<div id="wrapper">
<nav class="navbar-default navbar-static-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav metismenu" id="side-menu">

<!-- User admining -->
<li><a href="dashboard_2.html">users</a></li>
</ul>
</div>
</div>

database.js,我在其中连接到 firebase

var firebaseConfig = {
apiKey: "AIzaSyBWVRTc7stMysJVw4rjvzzn5-FnvMAE8_8",
authDomain: "proveneet.firebaseapp.com",
databaseURL: "https://proveneet.firebaseio.com",
projectId: "proveneet",
storageBucket: "proveneet.appspot.com",
messagingSenderId: "37090380224",
appId: "1:37090380224:web:a9e4c49d1e4b80bf"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

var database = firebase.database();

dashboard_2.html

 <!--===============================================================================================-->
<script src="https://www.gstatic.com/firebasejs/ui/3.6.1/firebase-ui-auth__es.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/3.6.1/firebase-ui-auth.css" />
<!-- The core Firebase JS SDK is always required and must be listed first -->
<!-- Add additional services that you want to use -->
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-messaging.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.9.1/firebase-functions.js"></script>


<!-- Firebase -->
<script src="js/manageusers.js"></script>

</head>

<body>
<div id="wrapper">
<nav class="navbar-default navbar-static-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav metismenu" id="side-menu">

<li class="active"><a href="dashboard_2.html">users</a></li>
</ul>
</div>
</div>



<button class="btn btn-primary btn-lg float-right" type="submit" id="addusr">Next</button>

manageusers.js,这是我想向数据库添加信息的地方

var connect = database.ref("/users");

如果我将database.js中的内容复制到manageusers.js并添加var connect = database.ref("/users");它告诉我该应用程序已经存在,如果不存在,它会告诉我没有创建 firebase 应用程序,我想在两个页面中使用相同的 firebase 数据库,但我不能这样做,有什么办法可以吗?

最佳答案

您需要防止您的应用再次初始化。

firebase.initializeApp(firebaseConfig); 更改为

if (!firebase.apps.length) {
firebase.initializeApp(config);
}

关于javascript - 如何在多个 HTML 页面中使用相同的 firebase 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56031356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com