首页 > 解决方案 > 无法读取未定义的属性“currentUser”。googleUser 值是否与 Google oauth2 中的 currentUser 相同?

问题描述

googleUser价值和currentUser谷歌一样吗auth2

componentDidMount() {

    window.gapi.load("client:auth2", () => {
      window.gapi.client
        .init({
          clientId:
            'CLIENT-ID.apps.googleusercontent.com',
          scope: "email"
        })
        .then(() => {
          this.auth2 = window.gapi.auth2.getAuthInstance()


          console.log('Current User Details :' , this.auth2.currentUser.Ab);
          console.log(this.auth2);

        });

       console.log(this.auth2.currentUser);
    }); 
  }

标签: reactjsoauth-2.0google-oauth

解决方案


一个 GoogleUser 对象代表一个用户帐户。GoogleUser 对象通常通过调用 GoogleAuth.currentUser.get() 获得。

GoogleAuth.currentUser.get() 返回代表当前用户的 GoogleUser 对象。请注意,在新初始化的 GoogleAuth 实例中,当前用户尚未设置。使用 currentUser.listen() 方法或 GoogleAuth.then() 获取初始化的 GoogleAuth 实例。

https://developers.google.com/api-client-library/javascript/reference/referencedocs


推荐阅读