Firebase: the data request results in empty

Currently experiencing confusion with my query to the Firebase database. As a beginner, I opted for a "simple tutorial" to grasp data reading. Creating a JSON object was successful and brought me joy. However, attempting to read data has become frustrating. My goal was to retrieve a single "record" where the email address matched a specific email. These email addresses are expected to be unique in a user database. Once I accessed the user, I aimed to utilize their UID generated by push(); for further actions. Since this is a demo and security is not a concern at this stage, the default read and write permissions of a newly created Firebase database apply - granting full access from anywhere to anyone.

This snippet represents the code of the demonstration site used to test functionality:

<body>
    <h1>retrieve data</h1>
    <div id="aaa" style="margin:20px;padding:20px;border-radius:30px;background-color:#6666FF;float:left">USERADD</div>
    <div style="clear:both"></div>
    <pre id="rawdata"></pre>
</body>

The above is the successful demo code that operated as intended.

var demofbdb = new Firebase("https://examples-sql-queries.firebaseio.com/user");

demofbdb
.startAt('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ffef1fef1ebdff9f6edfafdfeecfab1fcf0f2">[email protected]</a>')
.endAt('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d3dcd3dcc6f2d4dbc0d7d0d3c1d79cd1dddf">[email protected]</a>')
.once('value', function show(record) {
    $('#rawdata').text(JSON.stringify(record.val(), null, 4));
});

The following code is my own implementation which returns null without a clear explanation:

var demofbdb = new Firebase("https://<my-firebase-database>.firebaseio.com/Users");

demofbdb
.startAt('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3b3a3230713b3a3b3a323030311f3b3a2c3e2b3e31362c2b3a3171302d38">[email protected]</a>')
.endAt('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="086c6d6567266c6d6c6d65676766486c6d7b697c6966617b7c6d6626677a6f">[email protected]</a>')
.once('value', function show(record) {
    $('#rawdata').text(JSON.stringify(record.val(), null, 4));
});

This structure exemplifies the contents found within /Users:

"-JlyxV5xvQFLybevk9kD" : {
    // User details here
},
"-JlyxSJ8MciBNxguT415" : {
    // User details here
}

To clarify, the UIDs are directly beneath Users. Expanding on the UIDs reveals detailed information about each user.

The desired outcome of my query is to obtain the JSON object of the user identified by the first name "Demo" and last name "De Demoon".

Your assistance is greatly appreciated.

PS: While I have more experience with SQL databases than noSQL, circumstances have led me to explore the latter option. I believe noSQL databases prove advantageous when dealing with precomputed data that requires consistent joins. Storing these results in a noSQL database enhances lookup speed by minimizing unnecessary loads and grouping frequently requested datasets together. Feel free to correct any misconceptions in my understanding.

Answer №1

Within the demonstration you were observing, Michael Wulf applied the setPriority function to each record upon addition. If this step is skipped, it is usual to precede startAt with orderByChild. In your specific instance, implementing the following code will achieve the desired outcome:

var demofbdb = new Firebase("https://<my-firebase-database>.firebaseio.com/Users");

demofbdb
.orderByChild('E-Mail')
.startAt('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2145444c4e0f454445444c4e4e4f614544524055404f485255444f0f4e5346">[email protected]</a>')
.endAt('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7f7e7674357f7e7f7e767474755b7f7e687a6f7a7572686f7e753574697c">[email protected]</a>')
.once('value', function show(record) {
    $('#rawdata').text(JSON.stringify(record.val(), null, 4));
});

To configure the data akin to the demo, consider executing the subsequent steps:

function createDemoData(){
    var users = {
        "123": {
            "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="721913061d32141b0017101301175c111d1f">[email protected]</a>",
            "name": "Kato"
        },
        "234": {
            "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedfd0dfd0cafed8d7ccdbdcdfcddb90ddd1d3">[email protected]</a>",
            "name": "Anant"
        },
        "345": {
            "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="25484c464d44404965434c5740474456400b464a48">[email protected]</a>",
            "name": "Michael"
        },
        "456": {
            "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c777d6a657d5c7a756e797e7d6f79327f7371">[email protected]</a>",
            "name": "Kavya"
        }
    };

    var fbdbref = new Firebase("https://<my-firebase-database>.firebaseio.com/user");
    fbdbref.set( users );

    fbdbref.child("123").setPriority("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ee5effae1cee8e7fcebeceffdeba0ede1e3">[email protected]</a>");
    fbdbref.child("234").setPriority("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6a656a657f4b6d62796e696a786e25686466">[email protected]</a>");
    fbdbref.child("345").setPriority("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cca1a5afa4ada9a08caaa5bea9aeadbfa9e2afa3a1">[email protected]</a>");
    fbdbref.child("456").setPriority("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b101a0d021a3b1d12091e191a081e55181416">[email protected]</a>");
}

As a result, querying by email sans the inclusion of orderByChild is feasible, mirroring the functionality seen in the demo.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for showcasing background images sequentially?

css code #intro { position: relative; background-attachment: fixed; background-repeat: no-repeat; background-position: center top; -webkit-background-size: cover; -moz-background-size: cover; backgr ...

Preserving user interaction history in the browser while syncing with the server in AngularJS

My current challenge involves handling an HTML form that is connected to an object named a through an ngModel. When a user updates the data in the form, I send a PUT request to update the resource on the server. The response from the server contains update ...

The jQuery .find() method was not able to locate a valid

When implementing Bootstrap 4 nav tabs on my page, I encountered a situation where some tabs have embedded YouTube players. To address this issue, I created a function to detect when a tab is clicked and stop the video playback if the previous tab had a pl ...

Leveraging the power of TypeScript and Firebase with async/await for executing multiple

Currently, I am reading through user records in a file line by line. Each line represents a user record that I create if it doesn't already exist. It's possible for the same user record to be spread across multiple lines, so when I detect that it ...

The transmission of ContentType is unsuccessful

I'm having an issue with the code in my app. It seems that the content-type is not being sent. Is there a way to force it to be sent? $.ajax({ crossDomain: true, type: ...

What causes jQuery to not work correctly during keydown events?

I've been working on this external jQuery code: jQuery(document).one('keydown', 'g',function (evt){ if ($("#tb").html() == "0") { $("#tb").html("Testing the chicken.") } else {$("#tb").html("Chickens fart too." ...

Transform JSON data into a Java object with a modified format

I need to convert a JSON string representing an object into a Java object B with a different structure. My current approach involves creating a Java Object A that mirrors the JSON structure, using Jackson for JSON to A conversion, and Dozer with XML mappin ...

The submitHandler() function in the jQuery validate method is experiencing delays when executing and processing the form submission

Currently, I am using the jQuery validate method to validate my form. I have implemented some code in the submitHandler() method, but it seems to be taking longer than expected to execute. Can anyone provide me with a solution to resolve this issue? $(&ap ...

My selection of jQuery multiselect is experiencing issues with enabling disabled options

Incorporating the chosen jQuery plugin into my project has presented me with a challenge. The issue at hand is listed below. I have implemented a dropdown menu that includes both continents and countries in the same list. The specific scenario I am encou ...

React Warning: Every child component within a list must contain a distinct key property

Can you spot the issue in the following code snippet: <List> {sections.map(section => ( <> {section.header && <ListSubheader key={section.header}>{section.header}</ListSubheader>} {section.items ...

Accessing the router URL directly is proving to be ineffective

Currently, I am utilizing react-router in my project and everything is functioning perfectly except for when I directly access the URL. Specifically, if I click on a Link within the application that navigates to "/quizreview?quizId=nAUl3DmFlX", it works fl ...

Encountering an 'undefined' response when passing an array in Express's res.render method

After my initial attempt at creating a basic node app, I discovered that the scraper module was working correctly as data was successfully printed in the command window. Additionally, by setting eventsArray in index.js, I confirmed that Express and Jade te ...

Utilizing ObjectMapper to transform JSON into an Object representation

In my possession is an entity known as Organization. Class Phone{ String type; String number; String getType(){ return type; } String setType(String t){ type = t; } String getNumber(){ return number; ...

Having difficulties creating the JOLT schema for JSON transformation in NiFi

Attempting to utilize the jolt JSON to JSON transformation feature in Apache NiFi for converting one JSON format into another. The initial JSON data is as follows: { "total_rows": 5884, "offset": 0, "rows": [ { "id": "03888c0ab40c32451a01 ...

Troubleshooting the issue of onclick not functioning in JavaScript

My attempt to utilize onclick to trigger a function when the user clicks the button doesn't seem to be successful. For instance: function click(){ console.log('you click it!') } <input type='button' id='submitbutto ...

What is the method to retrieve the value from $.get()?

After searching on stackoverflow, I was unable to locate a solution to my issue and am currently unable to comment for further assistance. The dilemma I face involves extracting a return value from an asynchronous method to pass it onto another function: ...

Determine the length of a string in JavaScript and PHP taking into account "invisible characters" such as and

I have a textarea where users can input text, and I want to show them how many characters they have left as they type using JavaScript or jQuery: var area = 'textarea#zoomcomment'; var c = $(area).val().length; After that, the text is validated ...

A method to eliminate click events for an element

I have a unique element called #foo with a click event attached to it. This element is nested within another element called #bar, which also has a separate click event attached to it. <div id="bar"> <div id="foo" /> </div> Currently, ...

Mastering the Art of Merging 3 Arrays

Greetings to the Community I have a question regarding my code. I am looking to merge three variables together. $result = mysqli_query($con, "SELECT disease,age,SUM(CASE WHEN gender = 'm' THEN 1 ELSE 0 END) AS `totalM`, SUM(CASE WHEN gender ...

What is the best way to create a balanced 2 column grid layout in React Native?

I've been struggling with creating a 2 column list that's scrollable. I've tried using CSS Flex and FlatList, but I can't seem to get it right. Here's my database table structure: id title pic 1 First 4 A ...