0

In my HTML i have the following code:

<div id="userCount" class="number count-to" data-from="0" data-to="" data-speed="1000" data-fresh-interval="20"></div>

And in my Javascript i'm trying to get the registered users count to display it in ' data-to="" ' from firebase.

My Javascript code:

let db = firebase.firestore();
let count = 0;
var div = $('#userCount');

db.collection("users").get().then((snapShot) => {
   snapShot.forEach((doc) => {
      count++;
   });
});

div.data('to',count);

Why is that happening?

enter image description here

  • I think this is caused by the `let` keyword in declaration of the `count` variable. If you would use `var` keyword, it should exist throughout the contexts. I'm not sure tho – Encyklopedie Mar 24 '21 at 11:53

1 Answers1

0

Because db.collection function is asyncronized one. Please take a look at following solution

let db = firebase.firestore();
let count = 0;
var div = $('#userCount');

db.collection("users").get().then((snapShot) => {
   snapShot.forEach((doc) => {
      count++;
   });

   div.data('to',count);
});