How to check if firebase child contains any special value in JavaScript?
I am trying to get users who live in boston. Also I will use this method for other cities. So if it includes "bos" or "bo" I want to get the value anyway. I can't use equalTo
in this case, because the database contains this words different types like "bostonmelie" "bostonspek".
I tried to use code like this but it didn't work:
var myref= firebase.database().ref();
myref.child('Users').orderByChild('Mothclass').on("value", function(snapshot) {
//console.log(snapshot.val());
snapshot.forEach(function(data) {
//console.log(data.key);
var n = data.key.includes("bos");
console.log(n);
});
});
Console log
00jszBe7JKba7DHwZKbK1G9ID7D3 checkfraud.js:105:9
1Fnyqg0T7ldDEPGppu9zOIa66ZE2 checkfraud.js:105:9
2LbwNtF1vWW0AWiKRum45xD04812 checkfraud.js:105:9
2kB7IwMAlzWK6l5T9hWXHUoCiL42 checkfraud.js:105:9
3ASeCkjaxrQrPtelENfkm1XvZfJ3 checkfraud.js:105:9
3JmFZ9fztRbnFhTLIbscp97IXjB3 checkfraud.js:105:9
3i588ADWdsU4PX86wyNnk961GBC2 checkfraud.js:105:9
3p6tvDw3SfdOp2KptgzK0Padn143 checkfraud.js:105:9
3yAWS85t4xc8awxiZhE20qFkUGd2 checkfraud.js:105:9
javascript firebase firebase-realtime-database
|
show 11 more comments
I am trying to get users who live in boston. Also I will use this method for other cities. So if it includes "bos" or "bo" I want to get the value anyway. I can't use equalTo
in this case, because the database contains this words different types like "bostonmelie" "bostonspek".
I tried to use code like this but it didn't work:
var myref= firebase.database().ref();
myref.child('Users').orderByChild('Mothclass').on("value", function(snapshot) {
//console.log(snapshot.val());
snapshot.forEach(function(data) {
//console.log(data.key);
var n = data.key.includes("bos");
console.log(n);
});
});
Console log
00jszBe7JKba7DHwZKbK1G9ID7D3 checkfraud.js:105:9
1Fnyqg0T7ldDEPGppu9zOIa66ZE2 checkfraud.js:105:9
2LbwNtF1vWW0AWiKRum45xD04812 checkfraud.js:105:9
2kB7IwMAlzWK6l5T9hWXHUoCiL42 checkfraud.js:105:9
3ASeCkjaxrQrPtelENfkm1XvZfJ3 checkfraud.js:105:9
3JmFZ9fztRbnFhTLIbscp97IXjB3 checkfraud.js:105:9
3i588ADWdsU4PX86wyNnk961GBC2 checkfraud.js:105:9
3p6tvDw3SfdOp2KptgzK0Padn143 checkfraud.js:105:9
3yAWS85t4xc8awxiZhE20qFkUGd2 checkfraud.js:105:9
javascript firebase firebase-realtime-database
"it didn't work" is a useless problem description; if it had worked you wouldn't be here. What is logged by the code?
– Chris G
Nov 26 '18 at 9:54
The code turn "false" on console.
– Dorbagna
Nov 26 '18 at 9:55
I have never used firebase, but shouldn't you look indata.val()
for"bos"
? Isn'tdata.key
the unique ID made of "random" letters and numbers?
– Chris G
Nov 26 '18 at 9:57
1
So lets go back one step and doconsole.log(data.val());
And please, forget about the key, since we don't need it for now. You are searching for "bos", so the key is irrelevant.
– Chris G
Nov 26 '18 at 12:33
1
Yes, probably becausedata.val()
returns an object, not a string. The console shows the structure of the object, and you need to include the column name I guess. So something likedata.val().city.includes("bos")
? Do you still not see how this works?
– Chris G
Nov 26 '18 at 13:31
|
show 11 more comments
I am trying to get users who live in boston. Also I will use this method for other cities. So if it includes "bos" or "bo" I want to get the value anyway. I can't use equalTo
in this case, because the database contains this words different types like "bostonmelie" "bostonspek".
I tried to use code like this but it didn't work:
var myref= firebase.database().ref();
myref.child('Users').orderByChild('Mothclass').on("value", function(snapshot) {
//console.log(snapshot.val());
snapshot.forEach(function(data) {
//console.log(data.key);
var n = data.key.includes("bos");
console.log(n);
});
});
Console log
00jszBe7JKba7DHwZKbK1G9ID7D3 checkfraud.js:105:9
1Fnyqg0T7ldDEPGppu9zOIa66ZE2 checkfraud.js:105:9
2LbwNtF1vWW0AWiKRum45xD04812 checkfraud.js:105:9
2kB7IwMAlzWK6l5T9hWXHUoCiL42 checkfraud.js:105:9
3ASeCkjaxrQrPtelENfkm1XvZfJ3 checkfraud.js:105:9
3JmFZ9fztRbnFhTLIbscp97IXjB3 checkfraud.js:105:9
3i588ADWdsU4PX86wyNnk961GBC2 checkfraud.js:105:9
3p6tvDw3SfdOp2KptgzK0Padn143 checkfraud.js:105:9
3yAWS85t4xc8awxiZhE20qFkUGd2 checkfraud.js:105:9
javascript firebase firebase-realtime-database
I am trying to get users who live in boston. Also I will use this method for other cities. So if it includes "bos" or "bo" I want to get the value anyway. I can't use equalTo
in this case, because the database contains this words different types like "bostonmelie" "bostonspek".
I tried to use code like this but it didn't work:
var myref= firebase.database().ref();
myref.child('Users').orderByChild('Mothclass').on("value", function(snapshot) {
//console.log(snapshot.val());
snapshot.forEach(function(data) {
//console.log(data.key);
var n = data.key.includes("bos");
console.log(n);
});
});
Console log
00jszBe7JKba7DHwZKbK1G9ID7D3 checkfraud.js:105:9
1Fnyqg0T7ldDEPGppu9zOIa66ZE2 checkfraud.js:105:9
2LbwNtF1vWW0AWiKRum45xD04812 checkfraud.js:105:9
2kB7IwMAlzWK6l5T9hWXHUoCiL42 checkfraud.js:105:9
3ASeCkjaxrQrPtelENfkm1XvZfJ3 checkfraud.js:105:9
3JmFZ9fztRbnFhTLIbscp97IXjB3 checkfraud.js:105:9
3i588ADWdsU4PX86wyNnk961GBC2 checkfraud.js:105:9
3p6tvDw3SfdOp2KptgzK0Padn143 checkfraud.js:105:9
3yAWS85t4xc8awxiZhE20qFkUGd2 checkfraud.js:105:9
javascript firebase firebase-realtime-database
javascript firebase firebase-realtime-database
edited Nov 26 '18 at 11:34
droidBomb
58217
58217
asked Nov 26 '18 at 9:50
DorbagnaDorbagna
80111
80111
"it didn't work" is a useless problem description; if it had worked you wouldn't be here. What is logged by the code?
– Chris G
Nov 26 '18 at 9:54
The code turn "false" on console.
– Dorbagna
Nov 26 '18 at 9:55
I have never used firebase, but shouldn't you look indata.val()
for"bos"
? Isn'tdata.key
the unique ID made of "random" letters and numbers?
– Chris G
Nov 26 '18 at 9:57
1
So lets go back one step and doconsole.log(data.val());
And please, forget about the key, since we don't need it for now. You are searching for "bos", so the key is irrelevant.
– Chris G
Nov 26 '18 at 12:33
1
Yes, probably becausedata.val()
returns an object, not a string. The console shows the structure of the object, and you need to include the column name I guess. So something likedata.val().city.includes("bos")
? Do you still not see how this works?
– Chris G
Nov 26 '18 at 13:31
|
show 11 more comments
"it didn't work" is a useless problem description; if it had worked you wouldn't be here. What is logged by the code?
– Chris G
Nov 26 '18 at 9:54
The code turn "false" on console.
– Dorbagna
Nov 26 '18 at 9:55
I have never used firebase, but shouldn't you look indata.val()
for"bos"
? Isn'tdata.key
the unique ID made of "random" letters and numbers?
– Chris G
Nov 26 '18 at 9:57
1
So lets go back one step and doconsole.log(data.val());
And please, forget about the key, since we don't need it for now. You are searching for "bos", so the key is irrelevant.
– Chris G
Nov 26 '18 at 12:33
1
Yes, probably becausedata.val()
returns an object, not a string. The console shows the structure of the object, and you need to include the column name I guess. So something likedata.val().city.includes("bos")
? Do you still not see how this works?
– Chris G
Nov 26 '18 at 13:31
"it didn't work" is a useless problem description; if it had worked you wouldn't be here. What is logged by the code?
– Chris G
Nov 26 '18 at 9:54
"it didn't work" is a useless problem description; if it had worked you wouldn't be here. What is logged by the code?
– Chris G
Nov 26 '18 at 9:54
The code turn "false" on console.
– Dorbagna
Nov 26 '18 at 9:55
The code turn "false" on console.
– Dorbagna
Nov 26 '18 at 9:55
I have never used firebase, but shouldn't you look in
data.val()
for "bos"
? Isn't data.key
the unique ID made of "random" letters and numbers?– Chris G
Nov 26 '18 at 9:57
I have never used firebase, but shouldn't you look in
data.val()
for "bos"
? Isn't data.key
the unique ID made of "random" letters and numbers?– Chris G
Nov 26 '18 at 9:57
1
1
So lets go back one step and do
console.log(data.val());
And please, forget about the key, since we don't need it for now. You are searching for "bos", so the key is irrelevant.– Chris G
Nov 26 '18 at 12:33
So lets go back one step and do
console.log(data.val());
And please, forget about the key, since we don't need it for now. You are searching for "bos", so the key is irrelevant.– Chris G
Nov 26 '18 at 12:33
1
1
Yes, probably because
data.val()
returns an object, not a string. The console shows the structure of the object, and you need to include the column name I guess. So something like data.val().city.includes("bos")
? Do you still not see how this works?– Chris G
Nov 26 '18 at 13:31
Yes, probably because
data.val()
returns an object, not a string. The console shows the structure of the object, and you need to include the column name I guess. So something like data.val().city.includes("bos")
? Do you still not see how this works?– Chris G
Nov 26 '18 at 13:31
|
show 11 more comments
1 Answer
1
active
oldest
votes
To query the database for keys that start with bos
:
myref.child('Users').orderByKey().startAt('bos').endAt('bosuf8ff').on("value", function(snapshot) {
...
The uf8ff
is simply the last known unicode character, so you're selecting all keys in a range that starts with bos
and ends just before bot
.
You'll note that I removed the orderByChild()
call, since Firebase queries can only contain one orderBy...
. This approach performs the filtering on the server, but that means you must possibly reorder the matching nodes on the client.
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%2f53478463%2fhow-to-check-if-firebase-child-contains-any-special-value-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To query the database for keys that start with bos
:
myref.child('Users').orderByKey().startAt('bos').endAt('bosuf8ff').on("value", function(snapshot) {
...
The uf8ff
is simply the last known unicode character, so you're selecting all keys in a range that starts with bos
and ends just before bot
.
You'll note that I removed the orderByChild()
call, since Firebase queries can only contain one orderBy...
. This approach performs the filtering on the server, but that means you must possibly reorder the matching nodes on the client.
add a comment |
To query the database for keys that start with bos
:
myref.child('Users').orderByKey().startAt('bos').endAt('bosuf8ff').on("value", function(snapshot) {
...
The uf8ff
is simply the last known unicode character, so you're selecting all keys in a range that starts with bos
and ends just before bot
.
You'll note that I removed the orderByChild()
call, since Firebase queries can only contain one orderBy...
. This approach performs the filtering on the server, but that means you must possibly reorder the matching nodes on the client.
add a comment |
To query the database for keys that start with bos
:
myref.child('Users').orderByKey().startAt('bos').endAt('bosuf8ff').on("value", function(snapshot) {
...
The uf8ff
is simply the last known unicode character, so you're selecting all keys in a range that starts with bos
and ends just before bot
.
You'll note that I removed the orderByChild()
call, since Firebase queries can only contain one orderBy...
. This approach performs the filtering on the server, but that means you must possibly reorder the matching nodes on the client.
To query the database for keys that start with bos
:
myref.child('Users').orderByKey().startAt('bos').endAt('bosuf8ff').on("value", function(snapshot) {
...
The uf8ff
is simply the last known unicode character, so you're selecting all keys in a range that starts with bos
and ends just before bot
.
You'll note that I removed the orderByChild()
call, since Firebase queries can only contain one orderBy...
. This approach performs the filtering on the server, but that means you must possibly reorder the matching nodes on the client.
answered Nov 26 '18 at 15:31
Frank van PuffelenFrank van Puffelen
244k29387415
244k29387415
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%2f53478463%2fhow-to-check-if-firebase-child-contains-any-special-value-in-javascript%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
"it didn't work" is a useless problem description; if it had worked you wouldn't be here. What is logged by the code?
– Chris G
Nov 26 '18 at 9:54
The code turn "false" on console.
– Dorbagna
Nov 26 '18 at 9:55
I have never used firebase, but shouldn't you look in
data.val()
for"bos"
? Isn'tdata.key
the unique ID made of "random" letters and numbers?– Chris G
Nov 26 '18 at 9:57
1
So lets go back one step and do
console.log(data.val());
And please, forget about the key, since we don't need it for now. You are searching for "bos", so the key is irrelevant.– Chris G
Nov 26 '18 at 12:33
1
Yes, probably because
data.val()
returns an object, not a string. The console shows the structure of the object, and you need to include the column name I guess. So something likedata.val().city.includes("bos")
? Do you still not see how this works?– Chris G
Nov 26 '18 at 13:31