无法读取null的属性“ updatePassword”

时间:2018-07-03 21:16:58

标签: javascript html firebase firebase-authentication

我正在使用Firebase,并且正在尝试遵循文档以允许客户在登录时更新其密码,但是我不断收到以下错误消息:

updatePassword.js:10 Uncaught TypeError: Cannot read property 'updatePassword' of null
    at HTMLButtonElement.<anonymous> (updatePassword.js:10)
    at HTMLButtonElement.dispatch (jquery-3.3.1.js:5183)
    at HTMLButtonElement.elemData.handle (jquery-3.3.1.js:4991)

我知道有一个值,因为我将该值打印到了控制台。有人知道发生了什么吗?

HTML:

<h6>update password</h6>
<input id="updatePasswordField" class="updatePassword" contenteditable="true" />
<button id="updatePasswordBtn">Update Password</button>

JS

var user = firebase.auth().currentUser;

$(document).ready(function(){
  $("#updatePasswordBtn").click(function(){
    var password = $("#updatePasswordField").val();

    user.updatePassword(password).then(function() {
      // Update successful.
      console.log('updated');
    }, function(error) {
      // An error happened.
      console.log('failed update');
    });
  });
});

1 个答案:

答案 0 :(得分:0)

做完console.log(error.message);之后,我就知道了发生了什么。我需要做的就是再次重新验证用户身份。因此,我添加了一个新的输入字段,询问他们当前的密码,并将上面的代码片段放在reauthenticateAndRetrieveDataWithCredential()函数中。

此处的重新验证用户下的更多信息:

https://firebase.google.com/docs/auth/web/manage-users#set_a_users_password