简体   繁体   中英

Firebase Data Not Saving to Database

I'm having a problem attempting to add data to Firebase. Disregard the vars I don't use as I am trying different things and have commented out stuff I am not using at the moment.

But essentially, what I want to do is save this data to Firebase as follows:

  1. A user enters his name and age in input boxes and then hits submit.
  2. Once he/she hits submit, the function addFB() runs and ideally would create a child called users , and then under that, create a new child with the newly typed in userName (the User's name from the input box), and store his name and age in that child.

However, nothing is going to Firebase from this. Please help. Thanks in advance!

<h2>Add</h2>
<input type=text" id="userName">
<input type="number" id="userAge">
<button id="btUpdateMessage" margin-bottom="20px" onclick="addFB()"> Update</button>
&nbsp;
<script>
var lblCurrentMessage = document.getElementById('lblCurrentMessage'),
  userName = document.getElementById('userName'),
  btUpdateMessage = document.getElementById('btUpdateMessage'),
  userAge = document.getElementById('userAge'),
  rootRef = new Firebase('https://********.firebaseio.com');
var usersRef = rootRef.child('users');

function addFB() {
  usersRef.child(userName).set({
    Name: userName,
    Age: userAge
  });
  userName.value = '';
  userAge.value = '';
}

*rootRef = new Firebase('https://********.firebaseio.com');*

The above code is not correct. This is the old method.

Just check the below code. This will help you.

function addFB() {
    var userName = document.getElementById('userName'),
        userAge = document.getElementById('userAge');

    firebase.database().ref('users/' + userName).set({
       Name: userName,
       Age: userAge
    });
}

What about this?

 var rootRef = Firebase('https://********.firebaseio.com'); var contactsRef = rootRef .child('users'); function addFB() { contactsRef.child(userName) .push({ userName: document.getElementById('userName').value, userAge: document.getElementById('userAge').value }); Name.value = ''; Age.value = ''; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM