I am seeing a tad difference between child_added
and value
when returning data in firebase. Using value
I can test to see if snapshot.val()
has returned something or not using it like this:
Fetching Data:
ref.orderByChild('appUserName')
.equalTo(issuer)
.once('value')
.then(function (snapshot) {
var value = snapshot.val();
if (value) {
// Has Value...
}else{
//Has No Value...
}
Data Structure:
AppUsers --
234jl23jl4kj23 --
data --
//.. data
userName : "testUser1",
userPass: "password123"
9873h23foiu34u
//same structure
o8987s52df134o
//same structure
If I console.log
how the value has returned snapshot.val()
it returns the data on the level of the generated key:
{234jl23jl4kj23 --
{data --
//.. data
userName : "testUser1",
userPass: "password123"}}
If I get the data using child_added
:
ref.orderByChild('appUserName')
.equalTo(issuer)
.once('child_added')
.then(function (snapshot) {
var value = snapshot.val();
if (value) {
// Has Value...
}else{
//Has No Value...
}
It wont even go into the .then
function if issuer
is not found as a value to appUserName
, so I cant see in the firebase function if it got a value or not. Also the way child_added
gets data is one level deeper. Instead of returning at the generated key it returns the values in that key:
{data --
//.. data
userName : "testUser1",
userPass: "password123"}
I would prefer to use it this way because it would be one less loop I would have to check to first get the data inside of the key, then loop through the objects in the data spot. However if issuer
is not in appUserName
it wont go into the function for me to even do an if
else
Is there a way to drill in as deep as child_added
without looping but still be able to do an if else
to check if the snapshot.val()
has anything?
Firebase uses a very popular style of NoSQL database: Key-value stores. The concept is not so complicated once you know the basic JSON syntax.
*Realtime database store data only json format and it is specially used in app where data is synchronized concurrently like ola app(user location),sensex(Nifty) app where data not persist . *Firebase Storage just only store data like memory card.It is specially used for store backend data of app.
A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.
The child_added
event fires for each matching child under the node that you query. If there are no matching children, it will not fire.
This means that to test whether a node exists, you must use a value
handler. And since a query can have multiple results, you will need to loop over the children.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With