Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139654

Firebase update field using variable in javascript

$
0
0

I have a function which should go ahead and update the database on firebase

function editUser() {
  var userID = document.getElementById('userID').value;
  var editUserField = document.getElementById('editUserField').value;
  var newUserValue = document.getElementById('newUserValue').value;

  var database = firebase.database().ref().child('users/' + userID);
  database.once("value", function(snapshot){
      console.log(snapshot.val());
  })

  database.update({
    editUserField: newUserValue
  })

}

The above code is sort of working. Its getting the correct user, but whats not happening is the field is not getting updated, but instead, its creating a new field in the database and assigning it the value.

Looks like a key pair value is getting passed in

editUserField: newUserValue

but its actually taking the value editUserField

rather than getting getting it from the input:

var editUserField = document.getElementById('editUserField').value;

The value is actually getting stored correct from:

var newUserValue = document.getElementById('newUserValue').value;

But it doesnot update the value for the correct key, instead creates a new field called editUserField

I need it to get the values from the input and update the fields in firebase.


Viewing all articles
Browse latest Browse all 139654

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>