How to check if firebase child contains any special value in JavaScript?












0















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









share|improve this question

























  • "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't data.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 like data.val().city.includes("bos")? Do you still not see how this works?

    – Chris G
    Nov 26 '18 at 13:31
















0















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









share|improve this question

























  • "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't data.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 like data.val().city.includes("bos")? Do you still not see how this works?

    – Chris G
    Nov 26 '18 at 13:31














0












0








0








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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





    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 like data.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











  • 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








  • 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 like data.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












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer
























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer




























      2














      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.






      share|improve this answer


























        2












        2








        2







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 15:31









        Frank van PuffelenFrank van Puffelen

        244k29387415




        244k29387415
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Create new schema in PostgreSQL using DBeaver