Issue
I'm trying to order user points through Firebase's orderByChild() query method. I can't seem to wrap my head around it entirely because I'm a Typescript/Firebase newbie and so I've been using a lot of example codes.
My Firebase JSON looks like this:
{
"users" : {
"hJfEgXkyaKPckchL3kx8rpZ58Ew2" : {
"-LV6c8E5rRD4_mQqsb6Q" : {
"grade" : 11,
"name" : "Lin Manuel Miranda",
"points" : 100
}
},
"mlIBrdjT8CfIURQEAhLFPzzUFQg1" : {
"-LV7I6d8LeuWFLH5MRKy" : {
"grade" : 12,
"name" : "Emily Blunt",
"points" : 20
}
}
// "userID" : {
// "-firebase-generated-id": {
// "grade" : number,
// "name" : string,
// "points" : number
// }
// }
}
}
My code, albeit a bit messy, is running the way it is because I need to retrieve the specific keys to reference the database... So, instead I am trying to push all the information into a new array and then displaying the array inside the html.
// ts file
export class Tab1Page {
name: string;
grade: number;
points: number;
empList: Array<{name: string, grade: number, points: number}> = [];
constructor(
private db: AngularFireDatabase
) {
// the variable pkey here is a reference to the userID
const keyRef = firebase.database().ref('/users/' + pkey);
const ref2 = keyRef.orderByChild('points').limitToLast(7);
ref2.once('value').then(function(snap2) {
snap2.forEach(function (childSnap2) {
const key = childSnap2.key;
const List = this.empList;
const firebaseRef = firebase.database().ref('/users/' + pkey + '/' + key);
firebaseRef.once('value').then(function(snapshot) {
const name = snapshot.val().name;
const grade = snapshot.val().grade;
const points = snapshot.val().points;
List.push({
name: name,
grade: grade,
points: points
});
});
});
});
});
});
}
}
The problem is, no matter how I edit the code, the orderByChild() query returns my users in the order that they are in the database, NOT by their point values.
UPDATE For those who in the future may be struggling with this as well, the way I fixed it (with Frank's help), was instead of setting the User's ID as part of the JSON path, I pushed the ID as one of the childs:
{
"users" : {
"-LVL5EWo3MBnLdAv0fFf" : {
"ID" : "g4V3ZmBRJfcN2WSeoSPuPbxl3o72",
"grade" : 11,
"name" : "Caroline Choi",
"points" : 10
},
"-LVL69jiTxb5MprLpjrK" : {
"ID" : "21XBQvWXtuayo1ZxxmS566phDv13",
"grade" : 12,
"name" : "Emily Blunt",
"points" : 0
}
}
}
This allowed the query to run with no errors.
Solution
Since I'm not really sure what the problem is, I set up a little test of my own for you to look at.
JSON:
{
"-LV6c8E5rRD4_mQqsb6Q" : {
"grade" : 11,
"name" : "Lin Manuel Miranda",
"points" : 100
},
"-LV6c8E5rRD4_mQqsb6Z" : {
"grade" : 42,
"name" : "Frank van Puffelen",
"points" : 80
}
}
Live JSON: https://stackoverflow.firebaseio.com/54018652/users/hJfEgXkyaKPckchL3kx8rpZ58Ew2.json?print=pretty
code:
var ref = firebase.database().ref("/54018652");
var keyRef = ref.child("users/hJfEgXkyaKPckchL3kx8rpZ58Ew2");
var ref2 = keyRef.orderByChild('points').limitToLast(7);
ref2.once('value').then(function(snap2) {
snap2.forEach(function (childSnap2) {
var key = childSnap2.key;
var points = childSnap2.child("points").val();
console.log(key+": "+points);
});
});
Working code: https://jsbin.com/ceyejil/edit?js,console
When I run this code against this JSON it prints:
-LV6c8E5rRD4_mQqsb6Z: 80
-LV6c8E5rRD4_mQqsb6Q: 100
So these are the two nodes under the location that I'm querying in the order of their points value.
Answered By - Frank van Puffelen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.