简体   繁体   English

保存用户输入信息

[英]Saving User Input Information

I have a web app that I want to allow users to submit and save their information for their profile.我有一个 web 应用程序,我希望允许用户提交并保存他们的个人资料信息。 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.我试图保存两组信息,但它们没有存储在我的 firebase 数据库中的同一组中。 How can I get my function to work properly?如何让我的 function 正常工作? I have provided screenshots and the code below:我提供了屏幕截图和以下代码:

Submit Data Function and variables:提交数据 Function 和变量:

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: Firebase 数据库 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删除set( ) 仅当您想添加和生成新密钥和子项时才使用 push()

// 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()如果您只想在这两个中设置数据,则可以将.push()替换为.set()

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

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