Reputation: 431
I have a progress bar with three slides for registering users. When the first screen shows up user will enter primary info and click next and another form slides until the third one. I attached a function to the first next button to create new user in firebase database and a function to each next button to save the user's input into firebase DB under the created uid. I was to get this to work per instructions of firebase doc. However, noticed that each next button after the first save the data as expected but it replaces the everything under the uid with the newly entered input. I need it to add the new information to the existing one in DB and not replace it. I use this reference https://firebase.google.com/docs/database/admin/save-data I know the difference between push, setValue and update and I'm using update but it's replacing everything previously entered. What am I doing wrong and how can I get it to add new data to the existing entries? here are my functions
This the function attached the first Next button (creates user and obtain uid)
function registerBussiness() {
var database = firebase.database();
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var businessname = document.getElementById('Busname').value;
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
var website = document.getElementById('website').value;
//Get Elements
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
firebase.auth().createUserWithEmailAndPassword(email, password) .then(function(user) {
var uid = firebase.auth().currentUser.uid;
var auth = firebase.auth();
var storageRef = firebase.storage().ref();
var postData = {
fname: fname,
lname: lname,
BusinessName: businessname,
email: email,
website: website,
};
var updates = {};
updates['/Businesses/' + uid] = postData;
return firebase.database().ref().update(updates);
})
}
This is second function that replaces all the data previously entered by the first function
function addBussSumrytoDB() {
var BussSummry = document.getElementById('AboutUs').value;
var root = firebase.database().ref();
var uid = firebase.auth().currentUser.uid
var postData = {
BusinessSummary: BussSummry,
logoUrl: logoUrl.value,
BackgroundUrl: backgrdUrl.value,
};
var updates = {};
updates['/Businesses/' + uid] = postData;
return firebase.database().ref().update(updates);
}
Any help with be greatly appreciated
Upvotes: 1
Views: 2407
Reputation: 4739
In your case line updates['/Businesses/' + uid] = postData;
calls firebase to overwrite everything under '/Businesses/' + uid
path with new data. You need to specify pathes only those children that you actually want to update info at. Example below:
var updates = {};
updates['/Businesses/' + uid + '/' +'/BusinessSummary'] = BussSummry;
updates['/Businesses/' + uid + '/' +'/logoUrl'] = logoUrl.value;
updates['/Businesses/' + uid + '/' +'/BackgroundUrl'] = backgrdUrl.value;
firebase.database().ref().update(updates);
Upvotes: 3