0

I have a function like this:

$scope.doesUserLike = function(entryID, userID) {
    var userLike = false;
    $scope.likesRef.on('value', function(snap) {
        snap.forEach(function(like) {
            if(like.val().entryID == entryID && like.val().userID == userID) {
                userLike = true;
                console.log(userLike);
            }
        });
    });
    console.log(userLike);
    return userLike;
}

The console outputs true inside of the foreach loop but outside of the foreach loop it outputs false, I also tried $scope.userLike with the same result.

Jordash
  • 2,926
  • 8
  • 38
  • 77
  • Your data is being loaded asynchronously. By the time the "outer" `console.log` runs, the data hasn't loaded yet. See http://stackoverflow.com/questions/27049342/asynchronous-access-to-an-array-in-firebase – Frank van Puffelen Feb 09 '16 at 04:04
  • I tried the solution in the other question and it doesn't seem to work for a forEach loop, all of my variable assignments work unless they are in a forEach loop (which I'm using to query by multiple values) – Jordash Feb 09 '16 at 05:40

0 Answers0