【问题标题】:Where is the type Unsubscribe coming from?Unsubscribe 类型从何而来?
【发布时间】:2020-12-11 03:59:25
【问题描述】:

您好,如果用户可用或不在组件内,我正在尝试返回状态。 我要做的是return true 如果用户登录else false

我正在使用fire base给出的方法来跟踪用户的登录状态

firebase.auth().onAuthStateChanged

这是我的代码。

const isLoggedIn = firebase.auth().onAuthStateChanged(user => {
    console.log(!!user)
    return !!user;
  });

  const showSaveTheProgressButton:boolean = isLoggedIn;

但是当我尝试从这个方法返回布尔值时,我得到的是别的东西而不是布尔值

Type 'false | Unsubscribe' is not assignable to type 'boolean'.

上面是我从程序中得到的错误声明。 据我了解,该程序不应该返回Unsubscribe 对

【问题讨论】:

    标签: typescript firebase firebase-authentication


    【解决方案1】:

    您没有正确使用onAuthStateChanged。它不会返回您在其回调中返回的值。无论您传递给它的函数做什么,它总是立即返回一个取消订阅函数。每当登录和退出之间发生变化时,您都应该使用回调对用户的状态进行操作。

    如果您想要立即获取当前用户对象(如果有用户登录),则只需使用firebase.auth().currentUser。但是,您应该知道,当页面加载时,它总是会立即返回 null。您应该使用onAuthStateChanged 知道用户对象何时首次可用,这就是您想要知道的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 2011-04-18
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      相关资源
      最近更新 更多