Skip to content

Instantly share code, notes, and snippets.

@nandanmen
Created January 31, 2021 21:59
Show Gist options
  • Save nandanmen/ef234c6757bff0b6ec847d8910203c05 to your computer and use it in GitHub Desktop.
Save nandanmen/ef234c6757bff0b6ec847d8910203c05 to your computer and use it in GitHub Desktop.
Lightweight firebase demo (no frameworks)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div>
<h1>Counter</h1>
<p id="name">null</p>
</div>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.2.5/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.5/firebase-firestore.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script>
// Your web app's Firebase configuration
const firebaseConfig = {
/* get credentials from firebase */
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
// Add this user to the db first
/* db.collection("users")
.add({
first: "Ada",
last: "Lovelace",
born: 1815,
})
.then(function (docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function (error) {
console.error("Error adding document: ", error);
}); */
// Listen to a user
db.collection("users")
.doc("qqOQ5PLSM0z9EnxvweuK")
.onSnapshot(function (doc) {
const p = document.getElementById("name");
const { first, last } = doc.data();
p.innerText = `${first} ${last}`;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment