在Firebase JavaScript中保存数组或映射可以使用Firebase Realtime Database或Firebase Cloud Firestore来实现。
firebase.database().ref()
获取数据库的根引用。set()
方法将数组或映射保存到数据库中。示例代码如下:
// 引入Firebase库
<script src="https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.2/firebase-database.js"></script>
// 初始化Firebase
<script>
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
databaseURL: "YOUR_DATABASE_URL",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
firebase.initializeApp(firebaseConfig);
</script>
// 获取数据库引用
const database = firebase.database();
// 保存数组或映射
const arrayData = [1, 2, 3];
const mapData = { key1: "value1", key2: "value2" };
database.ref("arrayData").set(arrayData);
database.ref("mapData").set(mapData);
推荐的腾讯云相关产品:
firebase.firestore()
获取数据库的引用。set()
方法将数组或映射保存到数据库中。示例代码如下:
// 引入Firebase库
<script src="https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.2/firebase-firestore.js"></script>
// 初始化Firebase
<script>
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
firebase.initializeApp(firebaseConfig);
</script>
// 获取数据库引用
const firestore = firebase.firestore();
// 保存数组或映射
const arrayData = [1, 2, 3];
const mapData = { key1: "value1", key2: "value2" };
firestore.collection("collectionName").doc("documentId").set({
arrayData: arrayData,
mapData: mapData
});
推荐的腾讯云相关产品:
领取专属 10元无门槛券
手把手带您无忧上云