/
update_date_to_db.html
executable file
·99 lines (85 loc) · 3.57 KB
/
update_date_to_db.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Firebase Realtime Database</title>
<script src="https://www.gstatic.com/firebasejs/3.2.0/firebase.js"></script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script>
// 初始化 Firebase
var config = {
apiKey: "",
authDomain: "myfirebaseproject-57619.firebaseapp.com",
databaseURL: "https://myfirebaseproject-57619.firebaseio.com",
storageBucket: "myfirebaseproject-57619.appspot.com",
};
firebase.initializeApp(config);
// 建立 DB
var database = firebase.database();
function writeUserData(userId, name, email) {
firebase.database().ref('users/' + userId).set({
username: name,
email: email
});
}
function push(msg) {
var key = firebase.database().ref('message/').push({
message: msg,
}).key;
console.log("Push key = "+key)
}
function writeNewPost(uid, username, title, body) {
// A post entry.
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0
};
// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('posts').push().key;
// Write the new post's data simultaneously in the posts list and the user's post list.
var updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
$(document).ready(function(){
/* 建立使用者 */
$("#create_user").click(function(){
var id = $('#create_id').val();
var email = $('#create_email').val();
var name = $('#create_name').val();
writeUserData(id,name,email);
});
$("#push_test").click(function(){
var msg = $('#push_msg').val();
push(msg);
});
$("#update_test").click(function(){
var uid = $('#update_uid').val();
var username = $('#update_username').val();
var title = $('#update_title').val();
var body = $('#update_body').val();
writeNewPost(uid, username, title, body);
});
});
</script>
</head>
<body>
<h1>Firebase Realtime Database</h1>
<h3>建立使用者帳號</h3>
<input type="text" id="create_id" placeholder="ID"/>
<input type="text" id="create_email" placeholder="Email"/>
<input type="text" id="create_name" placeholder="名稱"/>
<button id="create_user">建立</button><br><br>
<input type="text" id="push_msg" placeholder="訊息"/>
<button id="push_test">Push</button><br><br>
<input type="text" id="update_uid" placeholder="UID"/>
<input type="text" id="update_username" placeholder="名稱"/>
<input type="text" id="update_title" placeholder="標題"/>
<input type="text" id="update_body" placeholder="內文"/>
<button id="update_test">發文</button><br><br>
</body>
</html>