TypeError:尝试在FBGraphRequestManager中分配给只读属性

时间:2019-11-19 12:32:34

标签: javascript facebook react-native facebook-graph-api

我已将Facebook登录信息集成到我的react本机应用程序中。成功登录后,我正在使用GraphRequest来获取用户个人资料。但是,我间歇性地看到以下错误,并且不确定为什么会发生? enter image description here

下面是我的FacebookService.js

的代码
import React from 'react'
import FBSDK from 'react-native-fbsdk'

const {
  LoginManager,
  AccessToken,
  GraphRequest,
  GraphRequestManager,
} = FBSDK;
class FacebookService {
    constructor() {
        this.requestManager = new GraphRequestManager()
    }

    loginToFacebook(callback)
    {
        LoginManager.logInWithPermissions(['email','public_profile',/*'user_friends'*/]).then(
            function(result) {
                if (result.isCancelled) {
                    alert('Login was cancelled');
                } else {
                    console.log(result)
                    alert('Login was successful with permissions: '
                    + result.grantedPermissions.toString());
                    AccessToken.getCurrentAccessToken().then(
                        (data) => {
                            //console.log(data);
                            callback(data.accessToken)
                            //alert(data.accessToken.toString())
                        }
                    )
                }
            },
            function(error) {
                //callback(error)
                alert('Login failed with error: ' + error);
            }
        );
    }

    logout()
    {
        LoginManager.logOut()
    }
    async fetchProfile() {
        return new Promise((resolve, reject) => {
        const request = new GraphRequest(
            '/me',
            null,
            (error, result) => {
                if (result) {
                    const profile = result
                    profile.avatar = `https://graph.facebook.com/${result.id}/picture?type=large`
                    resolve(profile)
                } else {
                    reject(error)
                }
            }
        )

        this.requestManager.addRequest(request).start()
        })
    }
}

export const facebookService = new FacebookService()

正如我上面提到的,问题是间歇性发生的,所以我真的不知道发生了什么。

1 个答案:

答案 0 :(得分:1)

我能够通过从构造函数中删除this.requestManager = new GraphRequestManager()的新实例并将其直接添加到requestGraph中来解决此问题,如下所示:

async fetchProfile(callback) {

    return new Promise((resolve, reject) => {
      const request = new GraphRequest(
        '/me',
        { 
          parameters: {fields: { string: 'picture.width(400),name' }}
        },
        (error, result) => {
          if (result) {
            const profile = result
            //profile.avatar = `https://graph.facebook.com/${result.id}/picture?&height=400&width=400`;
            resolve(profile)
          } else {
            reject(error)
          }
        }
      )

      new GraphRequestManager().addRequest(request).start()
    })
  }

让我知道这是否也可以解决您的问题。干杯!我可能会尝试使用React组件并在ComponentDidMount中初始化GraphRequestManager。