Untitled

 avatar
unknown
plain_text
2 years ago
3.0 kB
5
Indexable
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-firestore.js"></script>
</head>
<body>

<script>
    <!-- Firebase configuration -->
    const firebaseConfig = {

        apiKey: "AIzaSyAWsdtDNfeEN9wpX1BQXuD31_YjsEOW1UA",

        authDomain: "js-api-114a7.firebaseapp.com",

        databaseURL: "https://js-api-114a7-default-rtdb.firebaseio.com",

        projectId: "js-api-114a7",

        storageBucket: "js-api-114a7.appspot.com",

        messagingSenderId: "308067802078",

        appId: "1:308067802078:web:4126e562ddf51f2d709b4f"

    };

    firebase.initializeApp(firebaseConfig);
</script>

<!-- Firestore code -->
<script>
    const db = firebase.firestore();
    const messagesRef = db.collection('messages');

    function addMessage(text) {
        messagesRef.add({
            text: text,
            timestamp: firebase.firestore.FieldValue.serverTimestamp()
        }).then(function () {
            alert('Message has been added');
        }).catch(function (error) {
            console.error('Error writing message to Firestore:', error);
        });
    }

    function readMessages() {
      messagesRef.get()
       .then(querySnapshot => {
            querySnapshot.forEach(function (storedDocument) {
                const text = storedDocument.data().text;
                const timestamp = storedDocument.data().date;
                const id = storedDocument.id;

                console.log("Message:", text, timestamp, id)
            });
         })
      .catch(error => {
        console.error(error);
      });
   }

    // Read data on update
    function listenToMessageUpdates() {
        messagesRef.orderBy('timestamp').onSnapshot(function (querySnapshot) {
            querySnapshot.forEach(function (storedDocument) {
                const text = storedDocument.data().text;
                const timestamp = storedDocument.data().date;
                const id = storedDocument.id;

                console.log("Data updated:", text, timestamp, id)
            });
        });
    }

    function updateMessage(id, text) {
        messagesRef.doc(id).update({
            text: text,
            timestamp: firebase.firestore.FieldValue.serverTimestamp()
        }).then(function () {
            alert('Update successful');
        }).catch(function (error) {
            console.error('Error updating message in Firestore:', error);
        });
    }

    // Delete single message
    function deleteMessage(id) {
        messagesRef.doc(id).delete().then(function () {
            alert('Message was deleted. ID:' + id);
        }).catch(function (error) {
            console.error('Error deleting message from Firestore:', error);
        });
    }

    addMessage('Test2');
    readMessages();

</script>
</body>
</html>
Editor is loading...