Reputation: 4253
I have this to try to update an user's username in posts:
let postsRef = firebase.database().ref('posts/');
postsRef.child('username').update({'username': "josh2"})
The problem is post is under a key, like so:
posts
|
---- -MOFidfjdUSJFDJ
|
----- username: josh (username)
|
----- text: some text 1
|
---- -MOFifdjfhd
|
----- username: josh (username)
|
----- text: some text 2
---- -MOjfdjsiJidJ
|
----- username: marie (username)
|
----- text: some text
I'd like to update any posts
where username is josh
to josh2
. How can I do this?
Thanks a lot.
Upvotes: 0
Views: 128
Reputation: 80914
You can do the following:
var postRef = firebase.database().ref('posts').orderByChild("username").equalTo("josh");
postRef.on('value', ((snapshot) {
snapshot.forEach((subChild) => {
firebase.database().ref('posts').child(subChild.key).update({"username" :"josh2"});
});
});
https://firebase.google.com/docs/database/web/read-and-write
So here, you create a query to check if username is equal to josh, then you retrieve all this user's post, then using key
property you can get the post key and update.
Upvotes: 1