How to Update Particular Value of Child in Firebase Db

Firebase how to update child value in javasscript

2 problems:
1.You forgot to add "\eventos" on you child path.
2.dont use .set(), because it will delete all the other data.
Instead of .set() use .update().
Try this code:

firebase.database().ref('usuario')
.on('value',event =>{
event.forEach(user =>{
user.child('eventos').forEach(evento =>{
if (evento.val().categoryId === payload.id){
//Here is where i try to update the childe value, in my case category
let ref = firebase.database().ref('usuario/'+user.key+'/eventos/'+evento.key+'/'+evento.val().category)
.update(payload.name)
console.log(ref)
}

})
});

});

Let me know if it still dont work

Is it possible to update only specific data on my child on firebase?

Yes it is possible:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Accounts").child("user").child(useruid);
ref.child("date").setValue(newdate);
ref.child("email").setValue(newemail);
ref.child("name").setValue(newname);
ref.child("type").setValue(newtype);

If you have the list of names in a listview and you want to update them then, you can do the following:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String selectedFromList = (String) listview.getItemAtPosition(position);
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Accounts").child("user").child(useruid);
ref.orderByChild("name").equalTo(selectedFromList).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

//update values here
}
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});
});

How to update value of a particular child node in firebase realtime database?

Please note that Firebase database does not accept null values, so if you try to update a node with anything that results in null value, the node will be deleted. For more information, please check the documentation on https://firebase.google.com/docs/database/admin/save-data

I'm not sure if that is the reason behind your issue, I would need more information on what updatedPrice is, but try to log this variable and examine its contents, maybe it is resolving as a null value and causing the error.

Good luck!

How to update particular value of child in Firebase DB

For Updating values at a particular node in Firebase Realtime Database, use:-

  • You can either use runTransactionBlock:

      func updateTotalNoOfPost(completionBlock : (() -> Void)){

    let prntRef = FIRDatabase.database().reference().child("komal_kyz").child(your_AuroID).child("dealResul")

    prntRef.runTransactionBlock({ (resul) -> FIRTransactionResult in
    if let dealResul_Initial = resul.value as? Int{

    //resul.value = dealResul_Initial + 1
    //Or HowSoEver you want to update your dealResul.
    return FIRTransactionResult.successWithValue(resul)
    }else{

    return FIRTransactionResult.successWithValue(resul)

    }
    }, andCompletionBlock: {(error,completion,snap) in

    print(error?.localizedDescription)
    print(completion)
    print(snap)
    if !completion {

    print("Couldn't Update the node")
    }else{

    completionBlock()
    }
    })

    }

    While calling this function:-

    updateTotalNoOfPost{
    print("Updated")
    }
  • Or just call updateValues

        let prntRef  = FIRDatabase.database().reference().child("komal_kyz").child(your_AuroID)
    prntRef.updateChildValues(["dealResul":dealResult])

PS:- Prefer using runTransactionBlock: instead of .updateChildValues if you only want to increment a particular node. Also read this: -https://stackoverflow.com/a/39458044/6297658

How can I change the value of child in Firebase database?

If you've got the DataSnapshot for a path in the database, it's easy to get the DatabaseReference that you need to update it:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("BetSlip");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds: snapshot.getChildren()) {
String timestamp = ""+ ds.child("timeStamp").getValue();
String toggleStatus = ""+ ds.child("toggleStatus").getValue();
if (timeStamp.equals(timestamp) && toggleStatus.equals("on")) {
ds.child("toggleStatus").getRef().setValue("off");
}
if (timeStamp.equals(timestamp) && toggleStatus.equals("off")) {
ds.child("toggleStatus").getRef().setValue("on");
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});

Since you're updating the node based on its existing value, strictly speaking you might need to use a transaction for it.



Related Topics



Leave a reply



Submit