How do i add children to Firebase player UID? - firebase

I tried to add children nodes to a player with UID. I used child("node_1"). It creates only one node. How do I add items to this node?

To solve this, you need to have a unique identifier every time you try to add data to your database. Because a Firebase database is a NoSQL database and is structured as pairs of key and values, every node is a Map, which means in the case of Map, it replaces the old value with the new one.
Fortunately Firebase provides a method named push(). You can use it like this:
yourRef.push().child(name).setValue("John");

Which one you use add child node? Web, Ios or Android?
Alex Mamo was right: "you need to have a unique identifier every time you try to add data to your database"
Using for Web:
let node = firebase.database().ref().child(Your node)
var key= node.push().key // Get a key for a new
node.child(key).set(new value)
https://firebase.google.com/docs/database/web/read-and-write?authuser=0
Using for IOS:
let key = ref.child("posts").childByAutoId().key
https://firebase.google.com/docs/database/ios/read-and-write?authuser=0
And for Android:
String key = mDatabase.child("posts").push().getKey()
https://firebase.google.com/docs/database/android/read-and-write?authuser=0
Hope to help you.

Related

Can i search all push ids from the database without having to do it on the app side

I have a question about searching the database. Right now the database looks like this. first child = "hello" then second child is a pushid. and every push id has a few ids i want to look up. Can I somehow search through all push ids and return only the ones with the correct id or do i have to get all push ids and manually go through them on the app side?
FirebaseDatabase.instance.reference().child("hello").child(***this is the push id***).child("id to look up");
To recap: can I search all push ids on the database side and return only the children with the last id somehow?
If this doesn't make sense please tell me to clarify something.
To get only the child node under a list, you can do:
var ref = FirebaseDatabase.instance.reference().child("hello");
var query = ref.orderByKey().limitToLast(1);
And then you use that query to read the values, or bind to a widget.
To get nodes where a specific child property matches a specific value, you'd use this query:
var query = ref.orderByChild("id").equalTo("abc");

Firestore: Updating field in an object removes previous field

I am using Flutter cloud firestore. Here is how my database looks like.
I want to add more fields (like "2222") in a_numbers object. I use updateData()like this,
DocumentReference ref = Firestore.instance.document("products/-LMhR5cAyW4T0sa03UtU");
ref.updateData({"a_numbers": {"2222" : false}});
The above snippet basically deletes the previous value (1111) and then updates the database with 2222 field.
Any solution?
To update 'a_numbers' with Pair without removing the previous values, you should assign the reference path (example: 'a_number.2222') to K and assign value to V. I edited your code. please review it for more information.
DocumentReference ref = Firestore.instance.document("products/-LMhR5cAyW4T0sa03UtU");
ref.updateData({'a_numbers.2222': value});
This is the expected behavior, in your Flutter application, you need to store a_numbers in a Map and add any new (key,value) pairs using myMap.addAll({key:value}), then you can use ref.updateData(myMap)

How can i generate custom IDs for objects in firebase?

Please I want to give custom IDs to my database objects in Firebase but I don't now how to do it. Firebase creates default IDs for database objects which I don't want. I want to be able to assign my own IDs to objects or the child nodes of in the database for unique identification.
Most likely you're adding the items to the database with something like:
ref.push().set("my value");
This generates a new unique key under ref and sets your value on it.
If you want to use you own key/name for the child location, add the item with:
ref.child("my key").set("my value");
You cannot customize ID of firebase object, but you can create another field with ID role.
ref.child("my_id").set("customize_id");
after that, using "Filter by key" to get exactly your object you want.
In our case: We need to have a user_id type Int and auto-increase, so we can't use default _id of firebase object, we create user_id ourself to solve this problem.

Find object in Firebase by one value

I have next database list (usernames + user id).
How can i find object by user id and change his key (username)?
I'm using AngularFire2 with Angular 5.
You can find a child node by its value with a query like this:
var users = firebase.database().reference("usernames");
var query = users.orderByValue().equalTo("Sk6I..."ltA2");
By attaching a listener to this query you'll be able to find the reference and the key of the user (or "any users", since technically there may be more keys with the same value) matching the UID.
But you can't rename a node. You'll have to remove the existing node, and create a new one. For more on this see:
Firebase: Update key?
Firebase API for moving a tree branch from a collection to another one
Is it possible to rename a key in the Firebase Realtime Database?
Solution for my case:
this.db.list('usernames', ref => ref.equalTo(uid)).remove() // Remove old value
this.db.list('usernames', ref => ref.equalTo(uid)).set(
username, uid
) // Create new value

How do I stop Firebase from creating an additional nested object or how can I access the newly generated string?

Problem: Whenever I add an order to the orders array, an additional nested array element(-KOPWA...) gets added. I wouldn't mind except, I don't know how to access that nested string to access it's child nodes.
Example of database node for users below:
firebase.database().ref('users/'+userIdState+'/orders/'+<<unique numbervariable>>).push({
"order":{"test":"product","quantity":2}
});
I'm using the above code to push new json objects with a unique number to the firebase array. Still the nested array with the weird strings gets generated.
Can anyone help me understand how to either: create my own nested array with my own unique string or how to access the nested string that gets generated from firebase so I can access it's children nodes.
Multiple instances of nest arrays will be generated by users.
Any help is much appreciated.
Thanks,
Moe
You're experiencing this behaviour because Firebase's push is not the same as an array push. I recommend reading this article to understand how it works.
As for a solution, you can simply change push to set in your code. This will create the structure you were (presumably) expecting, that is
1:
order:
...
This is however potentially unsafe, if you allow concurrent writes (i. e. if the "unique number" in your example is not always unique).
Afaik Firebase recommends using push to safely create collections/"arrays". You can retrieve the generated key by calling the key property on the reference returned by push. Like this:
var ref = firebase.database().ref('users/'+userIdState+'/orders/'+<<unique numbervariable>>).push({
"order":{"test":"product","quantity":2}
});
var generatedKey = ref.key; // the value you're looking for
If you decide to use it, you can probably just drop the order number you have right now and just use the generated one.

Resources