1

I am trying to delete votes from my firebase db table and from my understanding it is an async request that takes time, though my function ends way before than so I get the following error -

Ignoring exception from a finished function

here is my function code -

function continueDeleteFromFirebaseVotesDB(videoId, contestId) {
    console.log("before query declaration");
    var query = database.ref("votes/" + contestId).orderByKey();
    console.log("before foreach loop")
    query.once(value)
        .then(function (snapshot) {
            console.log("inside foreach loop")
            if (!snapshot.exists) {
                console.log("votes db snapshot does not exist");
                return;
            }
            snapshot.forEach(function (childSnapshot) {
                //var key = childSnapshot.key;
                // childData will be the actual contents of the child
                var childData = childSnapshot.val();
                if (childData.video_id === contestId) {
                    childSnapshot.ref.remove();
                    console.log("removed vote - " + JSON.stringify(childSnapshot))
                    continueDeleteFromFirebaseVideosDB(videoId)
                }
            });
            return query;
        })
        .then(function (data) {
            console.log(JSON.stringify(data));
        })
}

What am I missing in order to make my function async and wait for the result?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Alon Shlider
  • 1,187
  • 1
  • 16
  • 46

2 Answers2

1

query.once(value) returns immediately with a promise, even though you call then on that promise. That means your function returns immediately, and the query finishes some time later. If your function performs async work, it should return a promise, and the caller should use that promise to collect any results that it contains.

You can't simply make async work asynchronous, if you were thinking about trying to make your function block until the query is complete. All promises must be handled correctly.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

Found the answer. I needed to write "value" and not value.

Alon Shlider
  • 1,187
  • 1
  • 16
  • 46