我是否需要在注销时调用 offAuth 方法?

Do I need to call the offAuth Method on logging out?

我创建了一个函数,该函数之前用于将用户详细信息存储在我的 Firebase 数据库的用户节点中。然而,在今天尝试它时,它现在返回一条错误消息说

TypeError: Cannot read property 'uid' of null

我编写的代码只是将 auth.uid 存储为用户的子节点,然后在该节点内存储该用户的所有数据。截至周五,这对一些用户有效,但现在它正在产生上述错误消息。

要解决这个问题,我需要在注销时调用 offAuth 方法吗?如果是这样,我该怎么做?

我的代码如下。

ref.onAuth(function(authData) {
    ref.child('user').child(authData.uid).set(authData).then(function(auth) {
        console.log('Data Saved Successfully!');
    }).catch(function(error) {
        console.log(error);
    })
})

只要用户的身份验证状态发生变化,就会调用onAuth() 方法。这意味着它在用户通过身份验证和未通过身份验证时都会被调用。在后一种情况下,authData 参数将是 null,您必须在代码中处理它。

来自Firebase documentation

使用 onAuth() 方法监听用户身份验证状态的变化。

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.onAuth(function(authData) {
  if (authData) {
    console.log("User " + authData.uid + " is logged in with " + authData.provider);
  } else {
    console.log("User is logged out");
  }
});

您的代码中缺少 if