Wes Haq
Wes Haq

Reputation: 73

cross-referencing two firebase arrays to obtain data

I was wondering if there's an easy way to pull out a value from one array based on its corresponding name.

enter image description here

Here, I've obtained the user email from the "coupons" collection. Now I would like to search through the "users" collection, find similar email, and output its corresponding "Name" ("Wes Haque Enterprises") to a $scope variable.

I already have references to both collections and $scope objects which have those references stored.

I just wanted to know if there's an easy way to traverse through the $scope.users object looking for the string "[email protected]" and then extracting "Wes Haque Enterprises" from it. Thanks.

Upvotes: 0

Views: 77

Answers (1)

Jay
Jay

Reputation: 35659

Assuming that you don't really want to iterate (traverse?) over a bunch of arrays but instead query for the data you need...

You can query the users node for the data you need. In MacOS:

FQuery *allUsers = [usersRef queryOrderedByChild:@"emailAddress"];
FQuery *thisUser = [allUsers queryEqualToValue:@"[email protected]"];
[thisUser observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
    for ( FDataSnapshot *child in snapshot.children) {
        NSLog(@"%@", child);
    }
}]

The result of the query will contain "Wes Haque Enterprises"

Or

ref.orderByChild("emailAddress").equalTo("[email protected]").on("child_added", function(snapshot) {
  console.log(snapshot.key());
});

Upvotes: 2

Related Questions