LẬP TRÌNH WEB HTML - CSS - JAVA SCRIPT
BÀI 17: FIRESTORE DATABASE TRONG WEB JS
Chèn đoạn Script này dưới thẻ body
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-firestore.js"></script>
Chèn config vào file js
const firebaseConfig = {
apiKey: "key",
authDomain: "web.firebaseapp.com",
databaseURL: "https://web-default-rtdb.firebaseio.com",
projectId: "web",
storageBucket: "web.appspot.com",
messagingSenderId: "id",
appId: "appid",
measurementId: "measurementid"
};
firebase.initializeApp(firebaseConfig);
let auth = firebase.auth()
let db = firebase.firestore();
Read All data
function GetAllDataOnce() {
db.collection("MachineDelivired").get().then((querySnapshot) => {
var machine = [];
querySnapshot.forEach(doc => {
machine.push(doc.data());
});
});
}
Read data as filter
function GetFilterData() {
store
.collection("yourCollection")
.where("yourIndex", "==", Bill)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
doc.ref.update({
image1: null,
image2: null,
image3: null,
image4: null,
image5: null,
});
});
})
.catch((e) => {
console.log(e);
});
}
Write new data
store.collection('Users').add({
email: localStorage.getItem("email"),
account: localStorage.getItem("account"),
location: localStorage.getItem("address"),
avatar: "https://www.pngitem.com.png",
time_registration: localStorage.getItem("registration"),
last_signin: localStorage.getItem("registration")
})
Write new data với id tùy chỉnh
store.collection("Users").doc(user.uid).set({
email: localStorage.getItem("email"),
account: localStorage.getItem("account"),
location: localStorage.getItem("address"),
avatar: "https://www.pngitem.com.png",
time_registration: localStorage.getItem("registration"),
last_signin: localStorage.getItem("registration")
})
Lấy một phần dữ liệu : source
firebase.firestore()
.collection("posts")
.orderBy("docName", "desc") // "desc": đảo ngược dữ liệu trong CSDL, bỏ nó đi dữ liệu trả về mặc định
.limit(10) // Chỉ lấy 10 cái từ dưới lên