Facebook登录获取错误。 "无法阅读财产' id'未定义"

时间:2017-07-12 11:24:11

标签: javascript facebook reactjs login native

我正在尝试将从facebook登录的数据登录到我的后端服务器,但是我在解决这个错误时遇到了困难,我似乎无法弄明白。

很可能有人可以帮助我,提前谢谢。

这是我的按钮功能:

 facebookSignin() {
    FBLoginManager.loginWithPermissions(['public_profile', 'email'], (error, data) => {
      if (error) {
        console.log(error);
      }
      let profile;
      try {
        profile = JSON.parse(data.profile);
        console.log(data.profile);
      } catch (error) {
        console.log(error);
      }

      /* signin on server with token */
      return fetch(`http://${this.serverHost}/users/signin/provider`, {
        method: 'POST',
        headers: new Headers({ 'Content-Type': 'application/json' }),
        body: JSON.stringify({
          provider: 'facebook',
          id: profile.id,
          email: profile.email,
          name: profile.name,
          token: data.credentials.token
        })
      })
        .then(data => data.json())
        .then(data => {
          /* get user data and auth token from server */
          console.log(data);
        });
    });
  }

我收到错误:

"无法阅读财产' id'未定义"

1 个答案:

答案 0 :(得分:1)

您解析try块中的数据,但尝试访问其外的person数据。

如果您JSON.parse收到错误,则变量persons仍为undefined。当您尝试阅读id道具时,您将收到错误"Cannot read property 'id' of undefined"

fetch;

之后将try代码块放在JSON.parse