Firebase query that returns objects - javascript

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());
});

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.

Firestore - Query, then update

I want to return an item with a specific ID and perform an update operation on it. My query gets the right result, but it won't let me update it.
I tried following this tutorial: https://www.youtube.com/watch?v=mYyPbfpoZeM
And read the documentation. Both didn't help. Other threads to the same topic are, well, different.
I have a database of objects which have a unique ID stored as integer.
I have an HTML form to get an ID as user input and the query below to retrieve the according object.
I tried this. The query worked, the update didn't.
db.collection('objects').where('ID','==', ID ).get().then((snapshot) => {
snapshot.docs.forEach( doc => {
console.log('debug');
console.log(doc.data().ID);
})
});
I am still new to firebase and js, so please forgive me if my code is uterly wrong.
I am currently stuck with this:
db.collection('objects').where('ID','==', ID ).get().then((doc) => {
console.table(doc);
});
Which is still not working.
For the second snippet an I currently get an unlegible table in which I can't really find the object I was looking for.
How do I update a single value in a single document?
EDIT: I forgot my implementation attempts of the update function.
I tried doc.update({value:0}) inside the for loop in snippet one which yielded doc.update is not a function. Similarly for doc.data().update(...).
In the second snippet I mainly tried to see what I got returned and ran variations of the above mentioned uodate function. With no success.
I managed to get it to work by studying the part of the firestore documentation that goes more into detail about the actual functions. Somehow it was hard for me to find this.
db.collection("users").where("name", "==", somename).limit(1).get().then(query => {
console.log(query);
const thing = query.docs[0];
console.log(thing.data());
let tmp = thing.data();
tmp.current_game_play = tmp.current_game_play + 1;
console.log(tmp);
thing.ref.update(tmp);
});
So I use where to get a Query object, use get to get a a querySnapshot inside the then promise resolve, use docs[0] to retrieve the first (and only) documentSnapshot and finally ref to get a reference that makes the whole thing updatable later.
try this:
var objectRef= db.collection("objects").doc(ID);
objectRef.update({
value: 0
}).then(function() {
console.log("Document successfully updated!");
}).catch(function(error) {
// The document probably doesn't exist.
console.error("Error updating document: ", error);
});

How to get the value of children in Firebase 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());
});
});

Firebase search by child value

I have the following structure on my Firebase database:
I would like to search for a user by name, last name or email but as I don't have the user key in the level above I don't know how I can achieve this. I'm doing and administrator session so it wouldn't have access to the user key.
I have tried:
let usersRef = firebase.database().ref('users');
usersRef.orderByValue().on("value", function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});
But it brings all the users on the database. Any ideas?
You can use equalTo() to find any child by value. In your case by name:
ref.child('users').orderByChild('name').equalTo('John Doe').on("value", function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});
The purpose of orderByChild() is to define the field you want to filter/search for. equalTo() can get an string, int and boolean value.
Also can be used with auto generated keys (pushKey) too.
You can find all the documentation here
A warning to avoid unpleasant surprises: when you use orderByChild and equalTo do not forget to add an index on your data (here's the doc)
If you don't all the nods will be downloaded and filtered client side which can become very expensive if your database grows.

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