javascript - 如何从 web 中的 firebase 实时数据库中获取数据?
问题描述
我创建了应用程序,安装了 CLI,将脚本添加到 index.html。然后我在实时数据库中添加了记录 {number: 1},登录并在项目文件夹中初始化了 firebase。
这是我的 index.html
<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-database.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
***config***
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.database();
console.log(db);
</script>
为什么我会得到这样的数据?为什么不 {number: 1}?
解决方案
// Get a reference to the database service
var database = firebase.database();
// This is reference to tha database, to fetch data from database use below code:
var starCountRef = firebase.database().ref('posts');
// Add ref of child if any
starCountRef.on('value', function(snapshot) {
console.log(snapshot.val());
});
参考:https ://firebase.google.com/docs/database/web/read-and-write
使用 val() 函数获取 json 格式的数据
推荐阅读
- python - 从列表中选择随机函数
- node.js - 邮件未送达,因为收件人的电子邮件提供商拒绝了它
- javascript - 如果数据不在 jQuery Filter 的列表中,如何不显示数据?
- c# - 这个 C#/.net5 表示法在插值字符串中用冒号调用是什么?
- python - 使用 ArrayStack 递归地展平嵌套列表
- javascript - Javascript 位置搜索为每个搜索重置而不是追加
- java - 为什么快捷方式 sout 在 IntelliJ IDE 中对我不起作用
- css - 如何增加引导卡的宽度和间距
- python-3.x - 无法使 python 在 conda 环境中工作(未找到 GLIBC)
- android - 如何手动更改firebase实时数据库、数据类型?