简体   繁体   English

如何将新数据添加到Firebase数据库中的现有数据?

[英]How can I add new data to existing data in a firebase database?

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. 我在第一个next按钮上附加了一个函数,以便在firebase数据库中创建新用户,并且在每个next按钮上附加了一个函数,以将用户的输入保存到创建的uid下的firebase DB中。 I was to get this to work per instructions of firebase doc. 我要按照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. 但是,请注意,第一个按钮之后的每个下一个按钮均按预期保存数据,但是它将uid下的所有内容替换为新输入的输入。 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. 我使用此参考https://firebase.google.com/docs/database/admin/save-data我知道push,setValue和update之间的区别,并且我使用的是update,但它取代了之前输入的所有内容。 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) 该函数附加了第一个Next按钮(创建用户并获取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 任何帮助将不胜感激

In your case line updates['/Businesses/' + uid] = postData; 在您的情况下,行updates['/Businesses/' + uid] = postData; calls firebase to overwrite everything under '/Businesses/' + uid path with new data. 调用firebase来用新数据覆盖'/Businesses/' + uid路径下'/Businesses/' + uid所有内容。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何能够将数据添加到Firebase数据库中 - How can I be able to add data into firebase database 如何在 Vue.js 和 Firebase 中向现有用户添加新数据 - How to add new data to existing user in Vue.js and Firebase 如何通过本机反应将新字段数据更新到现有文档而不覆盖firebase-9中的现有数据 - How can update new Field data to existing document without overwrite to existing data in firebase-9 by react native Firebase数据库规则-如何不覆盖现有数据 - Firebase Database Rules - how not to overwrite existing data 如何仅从 Firebase 获取没有现有数据的新数据? - How to only get new data without existing data from a Firebase? 如何覆盖 Firebase 中的现有数据? - How do I overwrite existing data in Firebase? 如何向 Firebase Firestore 中现有文档中的数组添加新索引? - How can i add new indexes to an array in an existing document in Firebase Firestore? 如何将数据访问器添加到JavaScript中的现有属性? - How can I add data accessors to an existing property in JavaScript? 如何避免添加与特定容器中现有数据相同的数据 - How can I avoid to add same data as an existing in the certain container localstorage 删除了以前的数据,如何添加新数据? - localstorage deletes the previous data, how can I add new data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM