Firebase set function keeps getting an error

Viewed 23

I have a function where I save the user's data and navigate to a new page, but I keep getting this error in the console. it only does this whenever I try to save the user's uid:

Failed to create studio. Cause: set failed: value argument contains undefined in property 'studiopick.studio.users.1k.uid'

I'm not

Here's my js code:

// Initialize Firebase
firebase.initializeApp(firebaseConfig);
  
// Initialize variables
const auth = firebase.auth()
const database = firebase.database()


//Save data formula
function handleNewStudioFormSubmit() { // renamed from newStudio
  // don't use alert - it blocks the thread
  console.log('debug: retrieving data... please wait')
  
  // Get data
  studioName = document.getElementById("studioName").value;
  email = document.getElementById("email").value;
  password =  document.getElementById("password").value;
  firstName = document.getElementById("firstName").value;
  lastName = document.getElementById("lastName").value;
  phoneNumber = document.getElementById("phoneNumber").value;

  console.log({ studioName, firstName, email }); // note added braces here
  
  // Validate input fields
  if (!validate_email(email) || !validate_password(password)) {
    // TODO: replace this alert with updating the form with an error message
    alert('Error with email or password')
    return false // cancel submission
  }

  if (
    !validate_field(firstName) ||
    !validate_field(lastName) ||
    !validate_field(phoneNumber) ||
    !validate_field(studioName)
  ) {
    // TODO: replace this alert with updating the form with an error message
    alert('One or More Extra Fields is Outta Line!!')
    return false // cancel submission
  };

  createStudioThenOpenDashboard({
    studioName,
    firstName,
    lastName,
    email,
    password,
    phoneNumber
  })
    .catch((err) => {
      console.error('failed to create studio: ', err);
      alert('Failed to create studio. Cause: ' + err.message);
    });

  return false; // prevent form's default action (reload page), so that the async tasks above can complete.
}

// ⇊ permits using await
async function createStudioThenOpenDashboard(data) {
  /* ... */

  const { email, password, firstName, lastName, phoneNumber } = data;

  // creates the user, and waits for it to finish being created
  const user = await auth.createUserWithEmailAndPassword(email, password);

console.log(user.uid);

  // writes data to the database, and waits for it to complete
  await firebase.database()
    .ref('/studiopick/studio/users/' + studioName) // <-- note fixed path
    .set({
      studioName : studioName,
      firstName : firstName,
      lastName : lastName,
      email : email,
      phoneNumber : phoneNumber,
      uid: user.uid
      // NEVER store passwords in plain text, even in testing
    });

  // once the above tasks succeed, navigate to the dashboard.
  window.location.href = "studiodash.html?id=" + uid
}
// ⇊ permits using await
async function createStudioThenOpenDashboard(data) {
  /* ... */

  const { email, password, firstName, lastName, phoneNumber } = data;

  // creates the user, and waits for it to finish being created
  const user = await auth.createUserWithEmailAndPassword(email, password);

  // writes data to the database, and waits for it to complete
  await firebase.database()
    .ref('/studiopick/studio/users/' + studioName) // <-- note fixed path
    .set({
      studioName : studioName,
      firstName : firstName,
      lastName : lastName,
      email : email,
      phoneNumber : phoneNumber,
      uid: user.uid
      // NEVER store passwords in plain text, even in testing
    });

  // once the above tasks succeed, navigate to the dashboard.
  window.location.href = "studiodash.html?id=" + uid
}

// Validate Functions
function validate_email(email) {
  expression = /^[^@]+@\w+(\.\w+)+\w$/
  if (expression.test(email) == true) {
    // Email is good
    return true
  } else {
    // Email is not good
    return false
  }
}

function validate_password(password) {
  // Firebase only accepts lengths greater than 6
  if (password < 6) {
    return false
  } else {
    return true
  }
}

function validate_field(field) {
  if (field == null) {
    return false
  }

  if (field.length <= 0) {
    return false
  } else {
    return true
  }
}



0 Answers
Related