firebase Query.orderBy* returns unordered list
I'm running tests to understands the behavior of of firebase Queries.
this is general function to return the query passed to it:
function logVal( query , tag ){
return query.on('value', function(snapshot) {
console.log( tag , snapshot.val());
});
}
in this test examples , the returned list is the same as my original data with no ordering!.
sRef = firebase.database().ref("students");
logVal ( "orderByChild" , sRef.orderByChild("bio") ) ;
logVal ( "orderByChild" , sRef.orderByChild("chem") ) ;
logVal ( "orderByChild" , sRef.orderByChild("physics") ) ;
logVal ( "orderByKey", sRef.orderByKey());
logVal ( "orderByValue" , sRef.orderByValue() );
this is my model data
{
students: {
"fahd": {
"physics": 9,
"chem" : 2,
"bio" : 0
}
,
"nasser": {
"physics": 8,
"chem" : 7,
"bio" : 6
}
,
"ahmad": {
"physics": 7,
"chem" : 5,
"bio" : 9
},
"ali": {
"physics": 9,
"chem" : 9,
"bio" : 9
}
,
"hus": {
"physics": 10,
"chem" : 5,
"bio" : 6
}
}
}
;
javascript firebase firebase-realtime-database
add a comment |
I'm running tests to understands the behavior of of firebase Queries.
this is general function to return the query passed to it:
function logVal( query , tag ){
return query.on('value', function(snapshot) {
console.log( tag , snapshot.val());
});
}
in this test examples , the returned list is the same as my original data with no ordering!.
sRef = firebase.database().ref("students");
logVal ( "orderByChild" , sRef.orderByChild("bio") ) ;
logVal ( "orderByChild" , sRef.orderByChild("chem") ) ;
logVal ( "orderByChild" , sRef.orderByChild("physics") ) ;
logVal ( "orderByKey", sRef.orderByKey());
logVal ( "orderByValue" , sRef.orderByValue() );
this is my model data
{
students: {
"fahd": {
"physics": 9,
"chem" : 2,
"bio" : 0
}
,
"nasser": {
"physics": 8,
"chem" : 7,
"bio" : 6
}
,
"ahmad": {
"physics": 7,
"chem" : 5,
"bio" : 9
},
"ali": {
"physics": 9,
"chem" : 9,
"bio" : 9
}
,
"hus": {
"physics": 10,
"chem" : 5,
"bio" : 6
}
}
}
;
javascript firebase firebase-realtime-database
Can you show the actual output so we can see for ourselves exactly what's happening? And can we see whatsRef
is?
– Doug Stevenson
Jan 1 at 21:48
sRef = firebase.database().ref("students"); & the output is the same as data models
– tabebqena
Jan 1 at 21:57
add a comment |
I'm running tests to understands the behavior of of firebase Queries.
this is general function to return the query passed to it:
function logVal( query , tag ){
return query.on('value', function(snapshot) {
console.log( tag , snapshot.val());
});
}
in this test examples , the returned list is the same as my original data with no ordering!.
sRef = firebase.database().ref("students");
logVal ( "orderByChild" , sRef.orderByChild("bio") ) ;
logVal ( "orderByChild" , sRef.orderByChild("chem") ) ;
logVal ( "orderByChild" , sRef.orderByChild("physics") ) ;
logVal ( "orderByKey", sRef.orderByKey());
logVal ( "orderByValue" , sRef.orderByValue() );
this is my model data
{
students: {
"fahd": {
"physics": 9,
"chem" : 2,
"bio" : 0
}
,
"nasser": {
"physics": 8,
"chem" : 7,
"bio" : 6
}
,
"ahmad": {
"physics": 7,
"chem" : 5,
"bio" : 9
},
"ali": {
"physics": 9,
"chem" : 9,
"bio" : 9
}
,
"hus": {
"physics": 10,
"chem" : 5,
"bio" : 6
}
}
}
;
javascript firebase firebase-realtime-database
I'm running tests to understands the behavior of of firebase Queries.
this is general function to return the query passed to it:
function logVal( query , tag ){
return query.on('value', function(snapshot) {
console.log( tag , snapshot.val());
});
}
in this test examples , the returned list is the same as my original data with no ordering!.
sRef = firebase.database().ref("students");
logVal ( "orderByChild" , sRef.orderByChild("bio") ) ;
logVal ( "orderByChild" , sRef.orderByChild("chem") ) ;
logVal ( "orderByChild" , sRef.orderByChild("physics") ) ;
logVal ( "orderByKey", sRef.orderByKey());
logVal ( "orderByValue" , sRef.orderByValue() );
this is my model data
{
students: {
"fahd": {
"physics": 9,
"chem" : 2,
"bio" : 0
}
,
"nasser": {
"physics": 8,
"chem" : 7,
"bio" : 6
}
,
"ahmad": {
"physics": 7,
"chem" : 5,
"bio" : 9
},
"ali": {
"physics": 9,
"chem" : 9,
"bio" : 9
}
,
"hus": {
"physics": 10,
"chem" : 5,
"bio" : 6
}
}
}
;
javascript firebase firebase-realtime-database
javascript firebase firebase-realtime-database
edited Jan 1 at 21:56
tabebqena
asked Jan 1 at 21:39
tabebqenatabebqena
3171312
3171312
Can you show the actual output so we can see for ourselves exactly what's happening? And can we see whatsRef
is?
– Doug Stevenson
Jan 1 at 21:48
sRef = firebase.database().ref("students"); & the output is the same as data models
– tabebqena
Jan 1 at 21:57
add a comment |
Can you show the actual output so we can see for ourselves exactly what's happening? And can we see whatsRef
is?
– Doug Stevenson
Jan 1 at 21:48
sRef = firebase.database().ref("students"); & the output is the same as data models
– tabebqena
Jan 1 at 21:57
Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what
sRef
is?– Doug Stevenson
Jan 1 at 21:48
Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what
sRef
is?– Doug Stevenson
Jan 1 at 21:48
sRef = firebase.database().ref("students"); & the output is the same as data models
– tabebqena
Jan 1 at 21:57
sRef = firebase.database().ref("students"); & the output is the same as data models
– tabebqena
Jan 1 at 21:57
add a comment |
2 Answers
2
active
oldest
votes
When you call snapshot.val()
the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.
So if you want to show the results in order, you need to process them using snapshot.forEach
:
return query.on('value', function(snapshot) {
snapshot.forEach(function(child) {
console.log( tag , child.val());
});
})
The second problem is that orderByValue
will only work if the child nodes have a primitive value. Since your nodes under users
are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.
thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.
– tabebqena
Jan 1 at 22:14
add a comment |
I find partial Solution :
when chnging the logVal function to :
function logVal( tag, query ){
return query.on('value', function(snapshot) {
console.log(tag)
snapshot.forEach(function(snapshot) {
var val = snapshot.val();
console.log("student: " + snapshot.key +
"bio: " + val.bio +
"physics: " + val.physics +
" chem: " + val.chem )
});
});
}
the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem
it still return the same data model in orderByKey
& orderByValue
.
the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log()
the whole object is converted to array which loss the order.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999153%2ffirebase-query-orderby-returns-unordered-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you call snapshot.val()
the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.
So if you want to show the results in order, you need to process them using snapshot.forEach
:
return query.on('value', function(snapshot) {
snapshot.forEach(function(child) {
console.log( tag , child.val());
});
})
The second problem is that orderByValue
will only work if the child nodes have a primitive value. Since your nodes under users
are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.
thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.
– tabebqena
Jan 1 at 22:14
add a comment |
When you call snapshot.val()
the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.
So if you want to show the results in order, you need to process them using snapshot.forEach
:
return query.on('value', function(snapshot) {
snapshot.forEach(function(child) {
console.log( tag , child.val());
});
})
The second problem is that orderByValue
will only work if the child nodes have a primitive value. Since your nodes under users
are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.
thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.
– tabebqena
Jan 1 at 22:14
add a comment |
When you call snapshot.val()
the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.
So if you want to show the results in order, you need to process them using snapshot.forEach
:
return query.on('value', function(snapshot) {
snapshot.forEach(function(child) {
console.log( tag , child.val());
});
})
The second problem is that orderByValue
will only work if the child nodes have a primitive value. Since your nodes under users
are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.
When you call snapshot.val()
the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.
So if you want to show the results in order, you need to process them using snapshot.forEach
:
return query.on('value', function(snapshot) {
snapshot.forEach(function(child) {
console.log( tag , child.val());
});
})
The second problem is that orderByValue
will only work if the child nodes have a primitive value. Since your nodes under users
are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.
answered Jan 1 at 22:12
Frank van PuffelenFrank van Puffelen
241k29384412
241k29384412
thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.
– tabebqena
Jan 1 at 22:14
add a comment |
thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.
– tabebqena
Jan 1 at 22:14
thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.
– tabebqena
Jan 1 at 22:14
thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.
– tabebqena
Jan 1 at 22:14
add a comment |
I find partial Solution :
when chnging the logVal function to :
function logVal( tag, query ){
return query.on('value', function(snapshot) {
console.log(tag)
snapshot.forEach(function(snapshot) {
var val = snapshot.val();
console.log("student: " + snapshot.key +
"bio: " + val.bio +
"physics: " + val.physics +
" chem: " + val.chem )
});
});
}
the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem
it still return the same data model in orderByKey
& orderByValue
.
the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log()
the whole object is converted to array which loss the order.
add a comment |
I find partial Solution :
when chnging the logVal function to :
function logVal( tag, query ){
return query.on('value', function(snapshot) {
console.log(tag)
snapshot.forEach(function(snapshot) {
var val = snapshot.val();
console.log("student: " + snapshot.key +
"bio: " + val.bio +
"physics: " + val.physics +
" chem: " + val.chem )
});
});
}
the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem
it still return the same data model in orderByKey
& orderByValue
.
the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log()
the whole object is converted to array which loss the order.
add a comment |
I find partial Solution :
when chnging the logVal function to :
function logVal( tag, query ){
return query.on('value', function(snapshot) {
console.log(tag)
snapshot.forEach(function(snapshot) {
var val = snapshot.val();
console.log("student: " + snapshot.key +
"bio: " + val.bio +
"physics: " + val.physics +
" chem: " + val.chem )
});
});
}
the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem
it still return the same data model in orderByKey
& orderByValue
.
the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log()
the whole object is converted to array which loss the order.
I find partial Solution :
when chnging the logVal function to :
function logVal( tag, query ){
return query.on('value', function(snapshot) {
console.log(tag)
snapshot.forEach(function(snapshot) {
var val = snapshot.val();
console.log("student: " + snapshot.key +
"bio: " + val.bio +
"physics: " + val.physics +
" chem: " + val.chem )
});
});
}
the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem
it still return the same data model in orderByKey
& orderByValue
.
the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log()
the whole object is converted to array which loss the order.
answered Jan 1 at 22:11
tabebqenatabebqena
3171312
3171312
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999153%2ffirebase-query-orderby-returns-unordered-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what
sRef
is?– Doug Stevenson
Jan 1 at 21:48
sRef = firebase.database().ref("students"); & the output is the same as data models
– tabebqena
Jan 1 at 21:57