Zubair Amjad
Zubair Amjad

Reputation: 761

Input Value doesn't save, when pushing onto array gives undefined value

I am trying to update the user account details in firebase but I have noticed that the input value for one of my fields keeps coming up as undefined even when I console.log it. I am working in two files one is a loginjs file in which I am defining the user input.

signUpForm.addEventListener('click', function (e) {
e.preventDefault();

isSigningUp = true;

var email = signUpEmailInput.value;
var password = signUpPasswordInput.value;
var displayNameUser = displayNameInput.value;
var userPrivateKey = signUpPrivateKey.value;

var user = firebase.auth().currentUser;
var photoURL = "https://www.gravatar.com/avatar/" + md5(email);

if (signUpPasswordInput.value !== signUpPasswordConfirmInput.value) {
    setSignUpError('Passwords do not match!');

} else if (!displayNameUser) {
    setSignUpError("Display Name is required!");

} else if (!userPrivateKey) {
    setSignUpError('You need to set a Private Key!');
} else {
    auth.createUserWithEmailAndPassword(email, password)
        .then(function (user) {
            user.updateProfile({
                displayName: displayNameUser,
                photoURL: photoURL,
                privateKey: userPrivateKey

            }).then(function () {
                // Update successful.
                window.location.href = 'chat.html';
            }).catch(function (error) {
                // An error happened.
                window.alert("Some unexpected error happened!");
            });

            user.sendEmailVerification().then(function () {
                // Email sent.
            }).catch(function (error) {
                // An error happened.
                window.alert("Email was not able to send!");
            });
        })
        .catch(function (error) {
            // Display error messages
            setSignUpError(error.message);
        });
}});

The weird thing is that the user input for my displayname and photoURL are working just fine, but when it comes to my private key user input it registers the input when it goes to the chat page and I do a console.log(user.privatekey) It says it is undefined.

In my chatjs file, thats when I am pushing the all the user profile information. The chatjs file basically allows a user to send a message, the message and all the user profile information gets stored onto the firebase database.

            messages.push({
            displayName: displayName,
            userId: userId,
            pic: userPic,
            text: myString.toString(),
            privatekey: user.privatekey,
            timestamp: new Date().getTime() // unix timestamp in milliseconds

        })
        .then(function () {
            messageStuff.value = "";

        })
        .catch(function (error) {
            windows.alert("Your message was not sent!");
            messageStuff;
        });

The thing again is that the privatekey does not get stored at all, which is what I am not understanding, since it is registering user input in the loginjs file but when I go to the chatjs file it keeps saying the value is undefiend. I have googled everywhere and I still haven't found a solution to it. Any help would be greatly appricated!

Upvotes: 1

Views: 289

Answers (1)

JeremyW
JeremyW

Reputation: 5272

It's because the Firebase user object you receive from Firebase is not customizable. When you call the createUserWithEmailAndPassword(email, password) method, it returns a specifically defined user object back to you - check out the docs for the properties of this object.

The properties displayName and photoURL both work because they are already properties of the user returned. privateKey is not an existing property of the Firebase user object, and Firebase doesn't know how to handle an update call for a property that isn't defined. Check out this question & answer where Frank explains that Users in Firebase aren't customizable - you need to store any extra info separately.

Upvotes: 2

Related Questions