Home > Back-end >  Saving User Input Information
Saving User Input Information

Time:08-04

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": "[email protected]",
          "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": {
    }
  }
}

CodePudding user response:

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()

  • Related