The Code (Cont.): Updation


  Updating user #1
ref.on( "value", function( snapshot ) {
function(snapshot) is a callback or callback function, which is any reference to executable code that is passed as an argument to another piece of code such as the on method here; that code is expected to call back (execute) the callback function as part of its job.
01<script>
02 //
03 // Function to get form values
04 //
05 function getInputVal( input ) {
06  return document.getElementById( input ).value;
07 }
08 
09 //
10 // Update a user.
11 //
12 function updateUser( ) {
13  // Get the input values.
14  var ID     = getInputVal( 'ID' );
15  var name2  = getInputVal( 'name' );
16  var email2 = getInputVal( 'email' );
17  var name3  = "";
18  var email3 = "";
19  var ref    = firebase.database( ).ref( 'users/' + ID );
20 
21  ref.on( "value",
22   function( snapshot ) {
23    const data = snapshot.val( );
24    name3  = Object.values(data)[1];
25    email3 = Object.values(data)[0];
26    if ( name2  ) { name3  = name2;  }
27    if ( email2 ) { email3 = email2; }
28    ref.set( { name: name3, email: email3 } );
29    var out  = ID + ": " + name3 + ", " + email3;
30    document.getElementById( 'result' ).innerHTML = out;
31   },
32   function ( error ) {
33    document.getElementById( 'result' ).innerHTML = "Error"; }
34  )
35  return( false );
36 }
37</script>