Firebase Realtime Database not saving data when I add "location.replace"

Solution 1:

The update function is asyncronous; it will take some time to complete. If you want to wait until the update is done, then you will need to use the promise it returns:

var updates = {};
updates['/users/' + document.getElementById('username').value] = data;
firebase.database().ref().update(updates)
  .then(() => {
    console.log('Saved successfully');
    location.replace('nextpage.html');
  });

Or with async/await:

async function someFunction () {
  var updates = {};
  updates['/users/' + document.getElementById('username').value] = data;
  await firebase.database().ref().update(updates);
  console.log("Saved successfully")
  location.replace("nextpage.html");
}