Keys in Firebase database that start with an underscore have been eliminated

As I was working on my web app, I noticed that the user object contains a key named _username which is placed at the beginning.

However, when I add this object to the firebase database, I cannot find the _username key - everything else seems to be present though.

I'm wondering why this particular key/value pair is not being stored in the database. After all, it is valid JSON. Are there any other solutions to this issue besides simply renaming it?

The structure of the object that I am pushing to an array looks like this:

{
    _username:"",
    history:[],
    // ...
}

Here is a snippet of my code:

var users = $firebaseArray(ref.child("user"));

// adding user
users.$add($scope.session.user);
// even after logging it afterwards, '_username' is still missing

// logging on initialization
users.$loaded().then(function (data) {
    $log.debug(data);
});

Everything else gets stored perfectly fine, except for the underlined variable...

Answer №1

It appears that when using $add() (specifically $firebaseUtils.toJSON()), properties beginning with an underscore are omitted. To demonstrate this, you can try:

console.log($firebaseUtils.toJSON(user));

To work around this issue, you can directly save the user object to the database using the JavaScript SDK:

ref.child("user").push(user);

This method will store all properties with appropriate key names, including those starting with an underscore.

Since AngularFire is based on the same JavaScript SDK, any modifications made will be immediately reflected.

Check out a demo on jsbin: http://jsbin.com/lobadej/edit?js,console

Answer №2

The decision to prefix private variables with "_" in JavaScript was made in order to align with a common convention used by developers. This choice differs from the default SDK behavior for AngularFire because it is designed to create extendable class instances that offer client-side functionality beyond basic data transfer.

The goal of using this naming convention is to simplify the process of extending and utilizing $firebaseArray and $firebaseObject, while also ensuring a seamless connection between saving data locally and syncing with the server.

In pre-ES6 environments where "$" is reserved by Angular libraries, the continued use of "_" as a prefix for private variables and methods remains appropriate.

If there are widespread challenges or issues experienced by users regarding this approach, we may reconsider our decision. However, since its implementation nearly 3 years ago, this is the first feedback we have received on this topic, so it appears to be working effectively as intended.

For more information, you can refer to: https://github.com/firebase/angularfire/issues/901

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

A guide to navigating a complex JSON structure using loops

I'm facing a challenge with a complex JSON object that I'm receiving from the server side. I'm unsure how to navigate through this object. Here is a snippet of the object: "message": { "name": ["Name cannot be blank."], ...

Creating a personalized button in DraftJs for inserting customized HTML with CSS styles

When utilizing DraftJs with WYSWYG, my task involves creating a custom button in the toolbar. Upon clicking this button, I intend to insert custom HTML (containing <button>) directly into the content. While I am familiar with adding a custom button ...

I can only use innerHTML once in my React application

I am attempting to clear my container div when the user clicks the search button in order to remove all existing search results. However, I am encountering an issue where using innerHTML to clear the container div only works the first time. If a second sea ...

The placement of the JavaScript OnChange Listener within an HTML document.LayoutControlItem in HTML

I'm facing issues with the OnChange listener not working as expected. When I place the script after the </form> tag, it works fine. However, if I put the script within the <head> tags, it fails to function properly. Due to limitations on ...

Unable to finish the real-time search request

Coding Challenge <form [formGroup]="searchForm" onsubmit="return false" id="searchField" ng-submit="" \"> <input type="text" placeholder="Search" formControlName="search" (keyup)="updateQuery()"/> <li *ngFor="let product o ...

Create an animated circle in canvas that smoothly descends over a set duration

I am looking to animate a circle using the ctx.arc(10, 10, 15, 0, Math.PI*2, true); method and have it flow downwards without losing its trail. The image below illustrates this clearly: As shown in the image, the circle is continuously moving downwards o ...

What are the drawbacks of adding a .js file with Express.js code to an HTML file?

Instead of an unresolved issue, I have a technical inquiry that I believe is worth exploring. Being a novice web developer, I tried to pass a variable from a .js file to an .html file in order to display it on the screen. My intention was to do this using ...

Obtain the search query stored in the state using ReactJS

I'm currently exploring ReactJS and attempting to retrieve the value entered in a search input box, then store it in a state. Here is my code snippet: <form className="search-form" onSubmit={this.handleSubmit}> <input type="search" na ...

Leveraging configuration files in AngularJS

I'm working on an Angular application that communicates with a Node.js backend Express application. I am using a config file to store environment variables for my Node app. Here's how I access the config file in my Node app: index.js var port = ...

Traversing through nested arrays within nested objects using AngularJS

In my project, I am utilizing angularjs 1 and dealing with a complex json object that has multiple levels of nesting. My goal is to use ng-repeat in order to access a nested array within the json structure. [{ "information": { "name": "simdi jinki ...

I would like to implement a delay on this button so that when it is clicked, there is a pause before navigating

When I add the delay, it doesn't work, and when I remove it, it does. What can I do to make this button have a href link that delays before redirecting to another page? (I'm a newbie) I need to click and wait 3 seconds before navigating to other ...

Verify whether the items within the ng-repeat directive already contain the specified value

I am facing an issue with displaying a JSON string of questions and answers in ng-repeat. The problem is that I want to display each question only once, but show all the multiple answers within ng-repeat. {Answer:"White",AnswerID:967,answer_type:"RADIO",f ...

Is it possible for JavaScript to access and read a local file from a web page hosted locally

I am interested in using html, css, and javascript to develop a user interface for configuring a simulation. This interface will be used to generate a visualization of the simulation and an output parameters file based on multiple input files. It is impor ...

Is it possible to switch from a dropdown menu to radio buttons?

I am looking to change the dropdown menu in my search section into radio buttons. Currently, when I select something from the dropdown menu, the search fields are altered. Here is the code for the dropdown menu: <select id="qs_category" name="qs_catego ...

Executing Javascript and C# functions through a single click

I need to call a method in Javascript and a method in C# when a button is clicked. First, I want to call the C# method called 'Find_Direction' on click, which takes two inputs. Then, I want to call the Javascript method named calcRoute. I attem ...

I'm struggling to incorporate MySql tables into my view using EJS. I can't pinpoint the issue

Trying to utilize the data on my EJS page is resulting in the following error: TypeError: C:\Users\ygor\Documents\VS Code\Digital House\PI-DevNap\src\views\user.ejs:22 20| <a class='p ...

The function for the Protractor promise is not defined

UPDATE: After some troubleshooting, I believe I have identified the issue causing it not to work. It seems that I am unable to pass arguments along when calling flow.execute(getSpendermeldung). Does anyone have a better solution than wrapping the ApiCall i ...

What is the process for calling CSS from a separate directory?

I am looking to start building a website. Can someone advise on how to organize html, css, and javascript into separate folders? One folder will house files specifically for writing HTML and JavaScript, while the other folder will be dedicated solely to ...

Delete a node from Firebase cloud function when the count reaches zero

Here's a function I have: module.exports.tagCountDecrement = functions.database .ref('/categoryTags/{tagName}/{postId}') .onDelete((snapshot, context) => { const tagsWithCountRef = admin .database() .ref('tags') ...

Sending a properly formatted string on Postman

My website allows users to answer coding problems. I am looking to store the questions and answers in a mongodb database. However, when testing the routes on my express application, I am encountering difficulties in sending formatted text in the request to ...