简体   繁体   中英

firebase .once is firing multiple times

My trial alerts keeps firing 4 to 6 times not sure why. I am using firebase with Electron but not sure what I am doing wrong. My trial alert function should only fire once

userRef
              .orderByChild("hardware_id")
              .equalTo(id)
              .once("child_added", function (snapshot) {
                // Check if user is on trial
                if (snapshot.val().status == "trial") {
                  if (
                    snapshot.val().last_session - snapshot.val().created >
                    604800 * 1000
                  ) {
                    // Trial Expired do something
                    PurchaseApp(
                      "Your trial has expired",
                      "To continue using ELMC Midi you must purchase an activation license",
                      true
                    );
                  } else {
                    // give access to software
                    var statusUpdate = {
                      last_session: firebase.database.ServerValue.TIMESTAMP,
                    };
                    database.ref("Accounts/" + id).update(statusUpdate);

                    //Trial alert
                    TrialAlert(snapshot.val().created);
                    // Access application
                    MainAppWindow(
                      "snap_log.val().message",
                      snapshot.val().status
                    );
                  }
                }

I'm actually nor sure if this is the expected behavior, but you can definitely stop it and save some bandwidth by adding a limit to the query:

userRef
  .orderByChild("hardware_id")
  .equalTo(id)
  .limitToFirst(1)
  .once("child_added", function (snapshot) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM