首页 > 解决方案 > console.log 什么都不返回

问题描述

我正在尝试设置一个登录屏幕,以便让用户通过 expo-facebook 通过 facebook 登录。我想在 console.log 中检索用户的姓名和电子邮件,以进行测试。但是,我在终端中什么都没有。你能告诉我为什么吗?但是,我的帐户可以正常连接。谢谢你的吹捧助手。

logInViaFacebook = async () => {
    try {
      await Facebook.initializeAsync("34904");
      const {
        type,
        token,
        expires,
        permissions,
        declinedPermissions,
      } = await Facebook.logInWithReadPermissionsAsync({
        permissions: ["public_profile", "email"],
      });
      if (type === "success") {
        // Get the user's name using Facebook's Graph API
        const response = await fetch(
          `https://graph.facebook.com/me?fields=id,name,email,birthday&access_token=${token}`
        );
        this.setState({
            signedIn: true,
            name: response.name,
            email: response.email,
          });
          console.log(this.state.name, this.state.email,)
          this.props.navigation.navigate("TabBar"); 
        // saving the token in AsyncStorage
        //await AsyncStorage.setItem('email', email); // <- save the email in AsyncStorage for later use
      } else {
        // type === 'cancel'
      }
    } catch ({ message }) {
      alert(`Facebook Login Error: ${message}`);
  }
}

标签: javascriptreact-nativefacebook-graph-api

解决方案


setState是异步的,因此您无法立即看到更新。要查看更新,您需要将回调作为 setState 的第二个参数传递。

this.setState({
   signedIn: true,
   name: response.name,
   email: response.email,
}, () => {
   console.log(this.state.name, this.state.email);
});
          

推荐阅读