My goal is to store new user information in my database using their unique user UID when they sign up through Firebase. Currently, my code is functioning properly, however, I am facing an issue where the name (which should be the created user UID) appears as "undefined" in my Firebase database even though the data being saved is correct.
You can view the undefined entries in my Firebase database here: https://i.stack.imgur.com/KVFBT.jpg
Below is the JavaScript code snippet I am using to save and create a new user:
// Firebase Variables
var auth = firebase.auth();
$(document).ready(function () {
//Passing parameters from form to Firebase
$('.addpic').on('submit', event => {
event.preventDefault();
const name = $('#name').val();
const hp = $('#hp').val();
const email = $('#email').val();
const password = $('#password').val();
const role = $('#role').val();
//Firebase user authentication
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(user => {
// Setting data into User database
firebase.database().ref('Admin/Person In Charge' + "/" + user.uid).set({
Name: name,
ContactNo: hp,
Email: email,
Password: password,
Role: role
}).then(success => {
console.log(user);
window.alert("Registered");
window.location = "user.html";
});
})
.catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
window.alert("Error : " + errorMessage);
});
});
});
I have tried various solutions to ensure that the data is saved under the UID of the created user but have not been successful. If you have any insights or suggestions, please let me know.