Reputation: 3
Hi I'm building an app using ionic and firebase and here is my firebase model:
{
"users" : {
"D972XU" : {
"8b91cc58-a962-4753-abd9-896b8a9418d5" : {
"email" : "[email protected]",
"house_key" : "D972XU",
"username" : "jim"
},
"8ea6537a-6385-4797-8fc3-62e9e5f06ac6" : {
"email" : "[email protected]",
"house_key" : "D972XU",
"username" : "george"
}
},
"V1OVU5" : {
"f4f283c0-f503-4d50-8af8-7a9ad592ca74" : {
"email" : "[email protected]",
"house_key" : "V1OVU5",
"username" : "john"
}
},
"YOKSPN" : {
"891bb612-4666-4095-be62-87c81f65c895" : {
"email" : "[email protected]",
"house_key" : "YOKSPN",
"username" : "jeff"
}
}
}
}
In the model, users are stored under a randomly generated string. My problem is that I'm trying to find the value of the string using this code in the firebase docs:
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.orderByChild("height").on("child_added", function(snapshot) {
console.log(snapshot.key() + " was " + snapshot.val().height + " meters tall");
});
My version:
app.factory('fireBaseData', function($firebase) {
var usersRef = new Firebase("https://mynextapp.firebaseio.com/users");
return {
ref: function() {
return ref;
}
};
});
app.controller('LoginCtrl', function($scope, $state, fireBaseData) {
fireBaseData.usersRef().child(authData.uid).orderByChild("email").on("child_added", function(snapshot) {
console.log(snapshot.key());
});
});
According to the docs, you shouldn't need to know the value of the specific dinosaur/string to access its details. However, the code doesn't work for me. Help please?
Note that the child nodes of /users, D972XU, V10VU5 and YOKSPN are randomly generated.
Upvotes: 0
Views: 795
Reputation: 35657
If you want to return a node (key) based on a child value, here's the code:
var ref = new Firebase("https://mynextapp.firebaseio.com/users/D972XU");
ref.orderByChild("username").equalTo("jim").on("child_added", function(snapshot) {
console.log(snapshot.key());
});
or for mail
var ref = new Firebase("https://mynextapp.firebaseio.com/users/D972XU");
ref.orderByChild("email").equalTo("[email protected]").on("child_added", function(snapshot) {
console.log(snapshot.key());
});
In this case, your /users node has direct children that are randomly generated and the data you are after is within those children. Because of that a query can't 'reach' your data as it's too deep. Even a Deep Query needs more info to work. So you have:
/users/random_node_name/uid
so this
fireBaseData.usersRef().child(authData.uid)
won't work because it leaves out the node between users and the authData.uid.
But - the solution is provided in the structure. The randomly generated node name is also stored within the data (a duplicate), which may eliminate the need to use it as a key.
Moving your data up one level is a solution so change the structure to:
{
"users" : {
"8b91cc58-a962-4753-abd9-896b8a9418d5" : {
"email" : "[email protected]",
"house_key" : "D972XU",
"username" : "jim"
},
"8ea6537a-6385-4797-8fc3-62e9e5f06ac6" : {
"email" : "[email protected]",
"house_key" : "D972XU",
"username" : "george"
},
"f4f283c0-f503-4d50-8af8-7a9ad592ca74" : {
"email" : "[email protected]",
"house_key" : "V1OVU5",
"username" : "john"
},
"891bb612-4666-4095-be62-87c81f65c895" : {
"email" : "[email protected]",
"house_key" : "YOKSPN",
"username" : "jeff"
}
}
}
As a side note, the structure I suggest is 'traditionally' how a /users node would be structured in Firebase. It has proven to be consistent, expandable and maintainable (and makes using Rules to secure your data much easier).
Your original structure is going to be a bear to make Rules work with.
Upvotes: 1