Simulate a Delete with Firebase - firebase

In the "Simulator" built into the Firebase site is it possible to simulate deleting a node?
I tried entering the path to a node in the URL field (e.g. /my/path/-JCNAUFZJFJMGX1RYWJL) and I entered {} into the data field but I think this just simulates adding nothing as apposed to deleting.

In Firebase, writing a null value is equivalent to removing the data at a given reference (i.e. ref.set(null) is effectively the same as ref.remove()), and thus using a null value is an effective way to test removing data in the simulator.

Related

Find the last element present in the array via Firestore rules

I am trying to work on restricting people who are able to like the posts on an application built on firebase.
I need to verify if the last element of the array exists.
However, I am unable to find the last element present inside array.
I have an array 'Likes' in cloud firestore which has 2 parts - userName and userId
How will I be able to find the last userId present in the array via firestore rules
I have tried below as can be done in python but doesnt work -
request.resource.data.likes[-1].userId
What you call an array, is actually known as a List object in Firestore security rules. This type indeed does not recognize negative offsets (honestly: most systems I know of don't), but you can get the same by using size.
So something like:
request.resource.data.likes[request.resource.data.likes.size()-1].userId

How do I create a Firebase collection?

So I am writing a chat application that I want to have multiple rooms, however, I can't find a button on the Firebase console that I can add child collections.
I've tried exporting, editing, then importing but that doesn't seem to do much. I have looked at some Firebase tutorial's but I can't find one that explains this.
Anything you enter in the console has to have a value itself, or at least one child (with a value). This is because Firebase does not explicitly store "null" or empty values in the database. You can enter the name of the collection and then rather than a value use the + button at the right to start adding children to it and so on until you reach a node with a value:
You cannot however simply create a placeholder for a collection that has no values. If you need a collection but can't initialize any of its data, just use your security rules to define what's allowed and write your client code knowing it may or may not exist. Firebase allows you to attach listeners to nodes that don't exist yet.

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.

How to not generate unique ID when using push method in Firebase?

I'm trying to push new data into Firebase but it keeps generating an unique ID and an extra child node for me. How can I stop generating the ID and extra node or is there another way to push data into Firbase? My dataset is given and I just need to dump the dataset into the db. (But I'm slowly updating this given dataset.)
Firease always generates the unique id when using the push method. You can use it as a key or as a parent node. This post describes push in more detail.
https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html
You should use set to publish new data to firebase if push doesn't fit your use case. There is also an update method as set will overwrite any data in the given path.
https://firebase.google.com/docs/database/web/save-data
I generally use set for new data, or if I change how I have my data structured, or if I am appending new data in a path, update to make changes to fields for a dataset, and push for sets of data that I want organized in a list by time( i.e. Chat message log ).

Replacing/Updating record in database without null values?

I am using the .AddOrUpdate-method for taking a model(Entity) and updating the existing record in my database. The problem is that I don't want the null values coming from the model to overwrite the existing values in the record. It's suppose to only overwrite the values that differ from the existing ones except the null-values coming form the model. What method/technique should I use for this. I dont wanna retrieve the record from the db and replace the null-values in the new object before putting it back into the db in order to do this.
Alos I don't wanna be forced to update single values in the record since several values in the model may be changed. Therefore I'm sending the entire object instead of sending values as parameters to the method executing the database commands.
Thanks alot. I hope my description makes some sence... Otherwise I'll try to elaborate :)

Resources