简体   繁体   中英

Saving User Input Information

I have a web app that I want to allow users to submit and save their information for their profile. There are two sets of information that I am trying to save, but they're not being stored in the same set in my firebase database. How can I get my function to work properly? I have provided screenshots and the code below:

Submit Data Function and variables:

var userEntry = firebase.database()
.ref('/Studiopick/studios/users');

var stuEntry = firebase.database()
.ref('/Studiopick/studios/studioId/stuInfo');

document.getElementById('studioForm')
.addEventListener('submit', submitForm);

document.getElementById('studioForm')
.addEventListener('submit', submitStu);

//Register A New User
function submitForm() {

    // Get values
    firstName = getInputVal('firstName');
    lastName = getInputVal('lastName');
    email = getInputVal('email');
    phoneNumber = getInputVal('phoneNumber');
    password = getInputVal('password');

    // Submit Profile Info
    saveUser(firstName, lastName, email, phoneNumber, password);
    document.getElementById('studioForm');
}

//Register A New Studio
function submitStu() {

    // Get values
    address = getInputVal('address');
    country = getInputVal('country');
    state = getInputVal('state');
    city = getinputVal('city');
    zip = getInputVal('zip');
   

    // Submit Profile Info
    saveStu(address, country, state, city, zip);
    document.getElementById('studioForm');
}

// Function to get get form values
function getInputVal(id) {
    return document.getElementById(id).value;
}
  
// Save new user to firebase
function saveUser(email, fullName, phoneNumber, userName, password) {
var newUserEntry = userEntry.push();
    newUserEntry.set({
        email:email,
        fullName:fullName,
        phoneNumber:phoneNumber,
        userName:userName,
        password:password,
        last_login : Date.now()

    });
}

// Save new/update studio to firebase
function saveStu(address, country, state, city, zip) {
    var newStuEntry = stuEntry.push();
        newStuEntry.set({
            address:address,
            country:country,
            state:state,
            city:city,
            zip:zip,
    
    });
}

Firebase Database JSON:

{
  "Studiopick": {
    "artists": {
      "users": {
        "-N7MP9BpXpmR2ZePAdHz": {
          "email": "1234@gmail.com",
          "fullName": "Lil Grmln",
          "last_login": 1658250109589,
          "password": "September19!",
          "phoneNumber": "123-456-7890",
          "userName": "Test "
        }
      }
    },
    "studios": {
      "studioId": {
        "stuInfo": {
          "address: "studio address",
          "city": "studio city",
          "state": "studio state",
          "country": "studio country",
          "zip": "studio zip",
          "studio": "studio name"
      },
      "users": {
    }
  }
}

remove set() use push() only if you want to add and generate new key and child

// Save new user to firebase
function saveUser(email, fullName, phoneNumber, userName, password) {
var newUserEntry = userEntry;
    newUserEntry.push({
        email:email,
        fullName:fullName,
        phoneNumber:phoneNumber,
        userName:userName,
        password:password,
        last_login : Date.now()

    });
}

// Save new/update studio to firebase
function saveStu(address, country, state, city, zip) {
    var newStuEntry = stuEntry;
        newStuEntry.push({
            address:address,
            country:country,
            state:state,
            city:city,
            zip:zip,
    
    });
}

if you want to set data in only in these two so you can replace the .push() with .set()

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