firebase host 4: write/read from web
Since setting up the firebase, I was searching lot of examples of read/write data from web with HTML and js...
bot none works for me.
I think the version changment is also a cause, but not sure where are the differences.
the version of firebase lib I used no is 6.2.4 and the examples are mostly 5.x.x, the version change also impact other java lib version and support.
after several fail, I decide to search most recent one.
But still, I need to modify some thing,
When you setup your firebase app and realtime, it will give you some setting to add at the end of body of your html, I need to add the 2nd one, the firebase-database.js, other when you do
the the following are the config you get from firebase
I'm very new to HTML, it took me a while to understand the structure of HTML...
here is my working sample, it write data and read out, key and other id data has been changed
for the basic part, to write data from web we can use:
bot none works for me.
I think the version changment is also a cause, but not sure where are the differences.
the version of firebase lib I used no is 6.2.4 and the examples are mostly 5.x.x, the version change also impact other java lib version and support.
after several fail, I decide to search most recent one.
- 26Day26 前端福音(1/4): Firebase-介紹 & 建立專案
- 27Day27 前端福音(2/4): Firebase-寫入&刪除資料
- 28Day28 前端福音(3/4): Firebase-讀取資料
- 29Day29 前端福音(4/4): Firebase-帳號系統&資料讀寫規則
But still, I need to modify some thing,
When you setup your firebase app and realtime, it will give you some setting to add at the end of body of your html, I need to add the 2nd one, the firebase-database.js, other when you do
var db = firebase.database();
it will give you an error.
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>
<!--following is needed to make it work-->
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-database.js"></script>
the the following are the config you get from firebase
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIgfdwahrewhgfshgd5R6-833mPsisFO8Ikc",
authDomain: "fb-try1.firebaseapp.com",
databaseURL: "https://ffdsy1.firebaseio.com",
projectId: "ffdsy1",
storageBucket: "",
messagingSenderId: "299468413032",
appId: "1:299468413032:web:fgf345t345df565b"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
I'm very new to HTML, it took me a while to understand the structure of HTML...
here is my working sample, it write data and read out, key and other id data has been changed
<!DOCTYPE html>
<html>
<head>
<title>firebase read</title>
</head>
<body>
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>
<!--following is needed to make it work-->
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-database.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyCKiwsp5423543233mPsisFO8Ikc",
authDomain: "fb-try1.firebaseapp.com",
databaseURL: "https://fb-try1.firebaseio.com",
projectId: "fb-try1",
storageBucket: "",
messagingSenderId: "299468413032",
appId: "1:299468413032:web:fc9543254f565b"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
<!--set data-->
<script>
var db = firebase.database();
db.ref("/chinese/Bob").set({
grade: 60
})
.then(function () {
alert("建立成功");
}).catch(function () {
alert("伺服器發生錯誤,請稍後再試");
});
</script>
<!--Read out data -->
<script>
var db = firebase.database();
db.ref("chinese/Bob").once('value', function (snapshot) {
var data = snapshot.val();
console.log(data);
});
</script>
<!--<script>src="index.js"</script>-->
</body>
</html>
for the basic part, to write data from web we can use:
- set: will erase old data
- update: will keep old data
- push: add data to data base with random key.
to delete, we can use set or remove
db.ref("/content").set({})
db.ref("/content").remove()
For the basic read, it's using snapshot function(link to google), A
DataSnapshot
contains data from a Database location. 2 method listed, there are some more, worth to read- once : only read once
then you get:
- on : read at first time and then run the call back function when data changed
I think there are some other ways, the query function are also important
留言
張貼留言