输入值不保存,当推入数组时给出未定义的值

时间:2018-06-25 21:06:32

标签: javascript firebase firebase-realtime-database

我正在尝试更新Firebase中的用户帐户详细信息,但是我注意到我的其中一个字段的输入值一直保持未定义状态,即使在我进行console.log记录时也是如此。我正在使用两个文件,一个是loginjs文件,其中定义了用户输入。

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);
        });
}});

奇怪的是,我的显示名和photoURL的用户输入工作正常,但是当涉及到我的私钥用户输入时,它会在进入聊天页面时注册该输入,然后执行console.log( user.privatekey)它是未定义的。

在我的chatjs文件中,这就是我推送所有用户个人资料信息的时间。 chatjs文件基本上允许用户发送消息,该消息和所有用户配置文件信息都存储在firebase数据库中。

            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;
        });

再说一遍,私钥根本不存储,这是我不了解的,因为它正在将用户输入注册在loginjs文件中,但是当我转到chatjs文件时,它总是说值是不确定的。我到处都有Google搜索,但仍然没有找到解决方案。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

这是因为您从Firebase收到的Firebase user对象不可自定义。当您调用createUserWithEmailAndPassword(email, password)方法时,它会向您返回一个专门定义的用户对象-check out the docs for the properties of this object

属性displayNamephotoURL都可以工作,因为它们已经是返回用户的属性。 privateKey不是Firebase用户对象的现有属性,并且Firebase不知道如何处理未定义属性的更新调用。请查看this question & answer,其中Frank解释说Firebase中的用户不可自定义-您需要单独存储任何其他信息。

答案 1 :(得分:0)

您很难看错字。更改:

privatekey:user.privatekey,

收件人:

privatekey:user.privateKey,

或:

privateKey:user.privateKey,

由于javascript区分大小写,因此您需要在变量名称中匹配字母K的大小写。