Issue
I'm looking to get data where two fields equal what I'm passing in.
Here's an example of my code:
this.refApp
.orderByChild('userUid')
.startAt(uid).endAt(uid)
.orderByChild('jobId')
.startAt(jobId).endAt(jobId)
.on('value', (snap) => {
//This currently doesn't get returned.
});
In the above example I don't get any compiler errors and code seems fine. However, I hard coded the data so that it would return an object where uid and jobid are equal to.
I can get this to work for one orderByChild but when I do two like above it doesn't seem to do anything.
Solution
You can only use one ordering method.
To query more, you'll need to rethink your data structure. Your current structure probably looks something like this:
{
"key": {
"id_1": {
"userUid": "user_1",
"jobId": "job_1"
},
"id_2": {
"userUid": "user_1",
"jobId": "job_2"
},
"id_3": {
"userUid": "user_2",
"jobId": "job_3"
}
}
}
With this structure you're limited to index off of one child key.
Now consider this structure:
{
"key": {
"user_1": {
"id_1": {
"jobId": "job_1",
"userUid": "user_1"
},
"id_2": {
"jobId": "job_2",
"userUid": "user_1"
}
}
"user_2": {
"id_3": {
"jobId": "job_3",
"userUid": "user_2"
}
}
}
}
This structure explicitly creates an index on uid
. So now if you want to get all the jobs by user you can write this query:
var ref = new Firebase('<my-firebase-app>');
var uid = 'user_1';
var userRef = ref.child('key').child(uid);
var query = userRef.orderByChild('jobId');
query.on('value', (snap) => console.log(snap.val());
Answered By - David East
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.