dimanche 21 mars 2021

Firestore get() method code is executing at the very end of the function

Below is my firestore code. you see the get method is executing at the very end of the function. Even though it is declared in the starting.

I am frustrating. if it is executing at the end then what is the use of this.

I don't know why firestore people are writing this type of codes. they don't have any answer to this in their website.

         function bookingsSubmit() { //This function saves the data of 
         //declaring database and all collections
         var db = firebase.firestore();
     const userColl = db.collection('users');
     const adminColl = db.collection('Admincrates');
     const bookingscoll = db.collection('bookings');
     const taskscoll = db.collection('tasks');

     //------Getting User Email
     var u = firebase.auth().currentUser.email;
     var username;
     var lngth;
     var curList = [];
     //---------Getting Username
     userColl.doc(u).get().then((doc) =>{ //This is going into user collection for user's name
              //We got all the fields of user and going through all the fields
                 username = doc.data().name;
                 console.log(username);
                 lngth = doc.data().currencyList.length;
                 for(var l = 0; l<lngth;l++){curList.push(doc.data().currencyList[l])}
             });

     console.log(curList);
     //---------Getting the Booking Id and Incrementing the booking Id value
     taskscoll.doc('bookingId').update({ //Increment
         id: firebase.firestore.FieldValue.increment(1)
     });
     var bkngId;
     taskscoll.doc('bookingId').get().then((doc)=> { bkngId = doc.data().id}); //Getting Booking Id and saving                              into a variable 
 console.log(bkngId);

 //---------Getting Table values.
  var tbl_user = document.getElementById('booking_tbl'); //Reading the table of the user page middle 
  for(var i = 1; i < lngth; i++) {

  }

 var docData = {
     email: u,
     name: username,
     bookingId: bkngId,
     timestamp: firebase.firestore.FieldValue.serverTimestamp(),
     arrayExample: [5, true, "hello"],
     nullExample: null,
     objectExample: {
         a: 5,
         b: {
        nested: "foo"
         }
     }
 };
 console.log(docData);
 bookingscoll.add(docData).then(() => {
console.log("Document successfully written!");
});

 }



Aucun commentaire:

Enregistrer un commentaire