Read/Write data in the child of a child Firebase HTML - javascript

Right now, I am able to write to my database like this:
var firebaseRef = firebase.database().ref();
firebaseRef.child(codeText).set(messageText);
and I can read data like this:
var firebaseReadRef = firebase.database().ref().child(child);
firebaseReadRef.on('value', function(datasnapshot){
var data = datasnapshot.val();
}
The problem is I can only store data like this http://i.imgur.com/hWfvUNE.png and I want to know how to store data like this http://i.imgur.com/Yui2wxp.png

firebaseRef.child(codeText).child("Child Name").set(messageText);
That way you get another child. You can also specify your reference like this:
firebase.database().ref("name/childName").set(messageText);
Hope it helped. You might want to check the documentation for more useful information.
https://firebase.google.com/docs/database/web/read-and-write

Related

How can I find a value of a state variable in React js when data is retrieved from Firebase database

I have happily managed to set my state variable named walletData with data from Firebase. Now I have a stupid problem that I can't get the value out, for example
let someNumber = this.state.walletData.walletSaldo
Console.log displays {saldo: 1500}. Firebase tree looks like this.
enter image description here
Did you try this?
this.state.walletData.walletSaldo.saldo

Firebase getting the reference

The full path for my reference looks like this
data/-KdWI6HAF0_wh9-NTEpe/films/thelobster/rating
and I can set it with
firebase.database().ref('data/-KdWI6HAF0_wh9-NTEpe/films/thelobster/rating')
.set(5);
The problem is that both the keys -KdWI6HAF0_wh9-NTEpe and thelobster are dynamic.
It makes it more difficult than it should be, is there any way to build this with just the -KdWI6HAF0_wh9-NTEpe key and make the film name a wildcard? Oris there a better way I am missing?
You can use Firebase's child() method:
firebase.database().ref('data').child(key).child('films').child(filmKey).child('rating').set(5);
Alternatively you can use ES6 string interpolation:
firebase.database().ref(`data/${key}/films/${filmKey}/rating`).set(5);
Firebase returns 'key' which is the id used to reference your object. Something like this.
var ref = firebase.database().ref("users/ada");
ref.once("value")
.then(function(snapshot) {
var key = snapshot.key; // "ada"
var childKey = snapshot.child("name/last").key; // "last"
});
You can read more about it here.
For 'thelobster', you can console.log() the data that comes from firebase, check the location of the dynamically changing 'thelobster', set it to a variable, and use the variable in your url.
Because I don't know how your incoming data looks like, I can't provide you with that code.

How to compare value and delete from firebase realtime database?

I am using Firebase realtime database in Android app, and have data like this:
How can I access that data from javascript.
I just wanna code function to get value of every endTime and compare with current time and if endTime <= Current Time then delete values yellow marked on anther image.
Events is fixed value.
Indjija, Vojvodina and -KsB2mVkpgQe_fRyFFH4 are auto generated so it's possible to have more values like that two.
I know how to solve that in android studio with Java code but I'm interesting about implementing server functions.
The answer to this question is buried deep within Firebase Database Reference.
forEach() is used to get to the child without knowing the child's exact path.
var leadsRef = database.ref('leads');
leadsRef.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
});
});
Now childSnapshot will contain the required data, the same thing can also be accessed using child_added.
leadsRef.on('child_added', function(snapshot) {
//Do something with the data
});
The only difference is that in case of forEach(), it will loop through from the start so if any new data will be added, it will also load the previous data but in case of child_added, the listener is only on new child and not the previous existing childs.
Useful Link:
Firebase Read and Write Web
Hope it helps.

Node.js: How can I get the reference for a Firebase Database Data Snapshot

I have a folder for each user. I have a DataSnapshot name "snapshot" that selects an item of the user that has been changed.
Using the following code, i get the snapshot for the item I want in the same user:
var locationSnapshot = snapshot.child("location");
This works. My question is how can I get the reference to this exact item in the database so that I can write it? Supposedly there is a function:
ref
non-null firebase.database.Reference
The Reference for the location that generated this DataSnapshot.
But when I use it, it doesn't work:
var locationReference = locationSnapshot.ref();
Question:
How do I use it or how do I get the reference?
In the latest Firebase SDK, ref is a property and not a function:
var locationReference = locationSnapshot.ref;

Firebase snapshot reference won't return data

I'm using firebase and have a list of people, with certain priorities. In one of my functions, setting and getting priorities is working fine. But in another, I can only set and trying to get the priority of the item returns 'dataSnapshot.getPriority() is not a function'.
var playersList = new Firebase('https://myfirebase.firebaseIO.com/players')
var winnerSnapshot = playersList.child(winner);
winnerSnapshot.setPriority('1300'); //This is working
var oldPriority = winnerSnapshot.getPriority(); //Not working
There are actually two different types of object at play here. A Firebase reference, and a DataSnapshot. When you call new Firebase(), you get a Firebase reference which allows you to write data (e.g. using set or setPriority) or attach callbacks for reading data (e.g. using on or once).
These callbacks registered with on() or once() receive the data via a DataSnapshot and you can call .getPriority() on that. Check out the Reading Data docs for full details.
For example, to make your example work, you could do something like:
var winner = "somebody";
var playersListRef = new Firebase('https://myfirebase.firebaseIO.com/players')
var winnerRef = playersListRef.child(winner);
// You use a firebase reference to write data.
winnerRef.setPriority('1300');
// You can also use a firebase reference to attach a callback for reading data.
winnerRef.once('value', function(winnerSnapshot) {
// Inside your callback, you get a DataSnapshot that gives you access to the data.
var priority = winnerSnapshot.getPriority();
});

Categories