Firebase - remove something by value - javascript

I'm creating advanced chatbot and I'm using Firebase to store names, chat bans, messages.
I want to remove something by value, so, if I banned user "test", i want to remove that same user with "test".
Here is my Firebase ban structure:
So, i want to remove "-KCXvmm_M-Nd7sR724hJ" by value (name), is that even possible?
ref: var banRef = new Firebase('application.firebaseio.com/ban');
Push: banRef.push({name:'test1'});

It should be a simple matter of:
ref.orderByValue().equalTo('test1').on('child_added', function(snapshot) {
snapshot.ref().remove();
});
Note that the query may match multiple children, in which case the child_added event will fire for each child and they'll all get removed.

Related

How can I access the child of a unique key in Firebase?

I am trying to access the child value of a unique key value (that had been "pushed") within Firebase. Currently, my database looks like this: I want to access the value of "emailOfUser"
I am very new to Firebase so I am not familiar with the functions. Currently, this is my method of obtaining other values for a different section of the database:
Thank you so much for any feedback!
I've tried different methods to accessing this data within the Firebase, but I cannot get it to work/the methods I were using were outdated. I also tried to "update" the Firebase instead of "pushing" the values to prevent a unique key from generating, but it simply overwrote my current data rather than appending something new.
If you want to load all the users who voted and print their emails, you can do that with:
get(child(dbref, 'usersWhoVoted')).then((snapshot) => {
snapshot.forEach((childSnapshot) => {
console.log(childSnapshot.key, childSnapshot.val().emailOfUser);
});
})
Note that your current structure allows a user to vote multiple times. If you want to only allow them to vote once, use some identifier of the user as the key in your database structure:
userVotes: {
"uniqueIdOfUser1": "valueTheyVotedOn",
"uniqueIdOfUser1": "valueTheyVotedOn",
...
}
Now each user can by definition only vote once, If they vote again (assuming your security rules allow that), their new vote will simply replace the existing vote.

How to only read changes in firebase realtime database

I have a database collection with readings, each new reading needs to be checked if it's out of the ordinary, if it is, there needs to be an alert sent.
So i'm using db.ref('collection').on('child_added', (child => { check(child); });
The problem with the .on function is that when the listener is added, all previous data is also read.
So how do i read a collection that only reads the changes in the database, also when the listener is first added? Or if that doesn't work, how do I differentiate the already added data with the new data?
The Firebase database synchronizes the state of whatever query or reference you attach your listener to. There is no option to only get new nodes built into the API.
If you want only new nodes, you will have to:
Ensure each node has an associated timestamp or order. If you're using Firebase's built-in push() keys, those might already serve that function.
Know what "new" means to the client, for example by either keeping the last timestamp or push key that it saw.
And then use a query to only request nodes after the stores timestamp/key.
So for example, if you only want to read nodes that are created after the moment you attach the listener, you could do something like this:
let now = db.ref('collection').push().key; // determine current key
db.ref('collection').orderByKey().startAt(now).on('child_added', ...)

Address Firebase object by child value, and then modify that object?

I'm facing a little difficulty finding information about how to modify objects in Firebase Realtime Database. I'm adding items into my database in real-time, so I won't know ahead of time what the object key is. As you know, the database structure looks like this:
Say I want to address a child of "testing", whose category is "social", what I do is this:
firebase.database().ref("testing").orderByChild("category").equalTo("social")
But how exactly can I then address this child so as to update this entire child (including all the fields - "category", "date", etc.) or even delete it? Thanks so much for any help!
To update (or delete) a node in Firebase you must know the complete path to that node. If you don't know the complete path, you can use a query to determine the node(s) matching a certain condition.
So in your case you'll need to execute the query, loop over the results, and update each child node in turn. In code:
let query = firebase.database().ref("testing").orderByChild("category").equalTo("Social");
query.once("value").then((results) => {
results.forEach((snapshot) => {
snapshot.ref.update({ propertyToUpdate: "new value" });
});
});
If you want to delete the matching node(s), the innermost line would be: snapshot.ref.remove().

How to loop through messages and delete certain ones in firebase?

I am using firebase and want to loop through my messages that I have and delete certain ones based upon a user's uid.
Here is an image of what I have for the structure of my data:
So far I know you would start of as something like:
Firebase.database().ref('messages').on('value', snapshot => {
snapshot.forEach(snap => {
if(snap.val().user.id === currentUser.uid){
//delete message here
};
});
});
Where do I go from here?
First of all, you probably want to use once() instead of on(). If you modify the contents of the database that you're working with, your on() will get triggered again for each change. You can see how that might be problematic for your case, if you only want to loop through the data once. Definitely learn about the difference between once() and on().
If you have a DataSnapshot type object, you can delete the contents of the database at its location with
snap.ref.remove()
Definitely read up on the Reference object type.

How to update an attribute object's attribute before running acceptance test?

First, we setup a scenario like so:
setupProject(server, []);
visit('/items');
This all works fine. The issue occurs when trying to update attributes of the current user prior to running the test.
Then update the current user with:
let user = server.create('user', 'organization', { enableManage: true });
This is intended to go to the specific user, go to an attribute object on that user called 'organization', and update an attribute of 'organization' called 'enableManage' to true.
Any help is appreciated.
You can always access Mirage's ORM via server.schema to mutate data in the database, prior to running a test.
let user = server.schema.users.find(1);
user.update({ organization: { enableManage: true });
That would update the organization property of this user record in the db.
If organization is an object you might want to do a clone, something like:
user.update({ organization: Object.assign(user.organization, { enableManage: true }));
By the way, depending on your API it looks like you might want to consider making organization a separate model, instead of a POJO that lives in each User's record.

Categories