Mr. Baks
Mr. Baks

Reputation: 317

How to insert data to firestore using javascript in web?

I am following the documentation properly and even the video that explains how does it work but for how many times I tried and even copied the exact code in the documentation I can't insert data on firestore database. Can you please tell me why is this happening? Also, there's no problem in my database because I already configure it read and write.

Here is my code below:

<!DOCTYPE html>
<html>
<head>
    <title>Login Admin</title>
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity></script>
</head>

<style type="text/css">

</style>

<body>
    <section class="container-fluid bg">
        <section class="row justify-content-center">
            <section class="col-12 col-sm-6 col-md-3">
                <form class="form-container">
                  <div class="form-group">
                    <label for="codeidtextfield">Code ID:</label>
                    <input type="text" class="form-control" id="codeidtextfield" aria-describedby="emailHelp">
                    <small id="emailHelp" class="form-text text-muted">Code ID's are provided by the Higher Ups. You must request first if you don't have one.</small>
                  </div>
                  <div class="form-group">
                    <label for="passwordfield">Password:</label>
                    <input type="password" class="form-control" id="passwordfield">
                  </div>
                  <div class="form-group form-check">
                    <input type="checkbox" class="form-check-input" id="exampleCheck1">
                    <label class="form-check-label" for="exampleCheck1">Remember Me</label>
                  </div>
                  <button type="submit" class="btn btn-primary btn-block" id="submit_btn">Submit</button>
                </form>
            </section>
        </section>
    </section>
</body>
<!-- Firebase CDN -->
<script src="https://www.gstatic.com/firebasejs/7.5.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.2/firebase-firestore.js"></script>
<script>

    //This config data are fine, I just removed it for the sake of privacy i guess.
    var config = {
        apiKey: 
        authDomain:
        databaseURL: 
        projectId: 
        storageBucket: 
        messagingSenderId:
        appId: 
        measurementId: 
    };
    // Initialize Firebase
    var app = firebase.initializeApp(config);
    // Initialize Cloud Firestore through Firebase
    const db = firebase.firestore(app);

    const docRef = db.collection("Admin").doc('CourierApplicationId').collection('CodeIDS');
    const codeidField = document.querySelector("#codeidtextfield");
    const passwordField = document.querySelector("#passwordfield");
    const submitButton = document.querySelector("#submit_btn");

    submitButton.addEventListener("click", function(){
        const codeid = codeidField.value;
        const passworddata = passwordField.value;

        console.log("I am going to authenticate: "+codeid+" and "+passworddata+" to Firestore database.");

        docRef.add({
        codeID: codeid,
        password: passworddata
        })
        .then(function(docRef) {
            console.log("Document written with ID: ", docRef.id);
        })
        .catch(function(error) {
            console.error("Error adding document: ", error);
        });
    });
</script>
</html>

Here is the sample log of the program when I tried to run it.

enter image description here

Upvotes: 1

Views: 3521

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Change this:

    // Initialize Firebase
    var app = firebase.initializeApp(config);
    // Initialize Cloud Firestore through Firebase
    const db = firebase.firestore(app);

into this:

    // Initialize Firebase
    var app = firebase.initializeApp(config);
    // Initialize Cloud Firestore through Firebase
    const db = app.firestore();

Use the firestore() method to connect to firestore:

https://firebase.google.com/docs/reference/js/firebase.app.App.html

Upvotes: 4

Related Questions