1

How can I update a document in Firestore on beforeunload?

    function sleep() {
      var start = new Date().getTime();
      while (new Date().getTime() < start + 3000);
    }

    window.addEventListener("beforeunload", function(event) {
      const ref = db.collection('users').doc(currentUser.uid);
      ref.update({
        profiles: true
      }).then(function() {
        console.log("Added!");
      });
      console.log("Blocking for 3 seconds...");
      sleep();
      console.log("Done!!");
    });

Seems like ref.update() is never called? Any idea why that's the case?

Student
  • 89
  • 1
  • 8

0 Answers0