How to get the value of children in Firebase Javascript? - javascript

This is my Firebase Database:
I need the URLs of the images that are associated alongside the unique random name generated by the push method. Is there any way I could do that? Also, there must exist a better way of sending data. Please, let me know. Thanks.
imgRef.on('value', function(snapshot) {
console.log(snapshot.val());
});
This is, as expected, returning the entire JSON object. I need the URL.

This is the most basic way to show the list of image URLs:
var rootRef = firebase.database.ref();
var urlRef = rootRef.child("user1/DAA Notes/URL");
urlRef.once("value", function(snapshot) {
snapshot.forEach(function(child) {
console.log(child.key+": "+child.val());
});
});

Related

how to check if value exists in realtime database firebase

I have created a realtime database on firebase and having no issues adding and removing data in tables etc.
I currently have it setup like this:
So my goal is to check if a given value is inside my database currently.
for example, I would like to check if 'max' is currently a username in my database.
var data = db.ref('loginInfo/');
data.on('value', function(snapshot) {
_this.users = snapshot.val()
});
That is how I get all the values, it is saved into _this.users
(How do i check if a value is inside this object, i am making a login program)
if i console.log the object, this is what I see:
image
If you want to check if a child node exists under loginInfo where the username property has a value of max, you can use the following query for that:
var ref = db.ref('loginInfo/');
var query = ref.orderByChild('username').equalTo('max');
query.once('value', function(snapshot) {
console.log(snapshot.exists());
});
I'd also recommend reading the Firebase documentation on queries, as there are many more options.

How do you get specific firebase data without knowing the unique id?

I have some data in my firebase database that I want to retrieve. I did one big push so that all instances would have a unique id. I know want to retrieve this data
I know I can do this:
var gymData = firebase.database().ref('gymData/previousWorkouts')
but when I get returned data like this:
how the hell am I suppose to access that id without knowing it?
for example a user of my app is going to search for an exercise and update it. they search by name, how do I find that id and look inside there? I don't get it :/
firebase.database().ref('gymData/exrcises').orderByChild('name').equal("barbell bench press").once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;//this is id
});
});

Firebase query that returns objects

I have the following Realtime Database structure
Now I want to query the aeds by their owner so that the query returns all aeds (with children) with the matching owner id.
But somehow I am not capable to do it although I feel it must be easy. Here is the code I have:
var aedRef = dbRef.ref().child("aeds")
var query = aedRef.orderByChild("owner").equalTo(userID);
console.log(query);
I feel that it should be easy but somehow I can't get it working. All I get is this:
e {repo: e, path: e, Me: e, Le: true}
Any help is greatly appreciated
query is just a document reference, it is not the result of the query. You need to use .on or .once on it to get it to return data.
Check the Firebase docs for more info on how to read and write data.
var aedRef = dbRef.ref().child("aeds");
var query = aedRef.orderByChild("owner").equalTo(userID);
// Get data and keep listening for changes
query.on('value', function(snapshot) {
console.log(snapshot.val());
});
// Only get data once
query.once('value').then(function(snapshot) {
console.log(snapshot.val());
});

Get initial collection values from Firebase

In my node app I try to get access to Firebase, which contains a few collections.
var firebase = require('firebase');
firebase.initializeApp({myConfig: "Here"});
var database = firebase.database();
var rootRef = firebase.database().ref()
How exactly do i get all rows of a particular collection or all collections in database? Printing those variables gives strange structured objects.
You should totally be looking into firebase documentation to get this information.
The way you retrieve will depend on what exact behavior you are expecting. And the documentation is excential to understand how firebase behave as a database in wich one of the possible cases.
var rootRef = firebase.database().ref().on('value', function(snapshot) {
console.log(snapshot.val());
});
Snippet above will look into any change on your entire database (since you are not specifying any child like ref().child("users")) and log it as a javascript Object.
Good luck and, again, go to the documentation. :)

Search inside objects javascript

I'm experimenting on login with firebase. I can make an account, and it'll store additional information too. The problem is retrieving this information.
I can get it using:
usersRef.on("value", function(snapshot) {
console.log(snapshot.val())
}, function (errorObject) {...});
When I do this I get two things (because I have two accounts):
-JrrzEOqZQU0HVeYVXCm: Object, has uid: "simplelogin:21" inside of it
-JrrzgOgQY2z6tNYN0BY: Object, has uid: "simplelogin:22" inside of it
Inside of the second object is the info that I need:
I received simplelogin:22 from the login.
Is there a way I can search inside of the objects to their uid, and get the rest of the information stored inside of that object?
Here's a fiddle
var thisAuthData = authData.uid;
//console.log(authData)
var usersRef = new Firebase("https://fiery-heat-xxx.firebaseio.com/users");
usersRef.on("value", function(snapshot) {
for(var amount in snapshot.val()){
console.log(snapshot.val()[amount].uid);
//here some if statement thingy's to check with your authData but you can do that yourself I guess ;)
}

Categories

Resources