将承诺与 GraphRequestManager 一起使用

gho*_*der 6 es6-promise react-native react-redux react-native-fbsdk

有没有人有关于如何在 GraphRequestManager 中使用 promise 的例子?我在我的动作创建者中得到了无法读取属性然后未定义的错误。

function graphRequest(path, params, token=undefined, version=undefined, method='GET') {
return new Promise((resolve, reject) => {
    new GraphRequestManager().addRequest(new GraphRequest(
        path,
        {
            httpMethod: method,
            version: version,
            accessToken: token
        },
        (error, result) => {
            if (error) {
                console.log('Error fetching data: ' + error);
                reject('error making request. ' + error);
            } else {
                console.log('Success fetching data: ');
                console.log(result);
                resolve(result);
            }
        },
    )).start();
});
Run Code Online (Sandbox Code Playgroud)

}

我使用我的动作创建者调用上面的

export function accounts() {
return dispatch => {
    console.log("fetching accounts!!!!!!");
    dispatch(accountsFetch());
    fbAPI.accounts().then((accounts) => {
        dispatch(accountsFetchSuccess(accounts));
    }).catch((error) => {
        dispatch(accountsFetchFailure(error));
    })
}
Run Code Online (Sandbox Code Playgroud)

}

我在控制台中收到“成功获取数据:”以及错误前的结果。这样 API 调用就成功了。错误是在 fbAPI.accounts().then((accounts) 中获取帐户之后,我认为这是由于 GraphRequestManager 立即返回而不是等待。

zag*_*goa 3

我有一个解决方案给你。我的提供者看起来像这样:

 FBGraphRequest = async (fields) => {
        const accessData = await AccessToken.getCurrentAccessToken();
        // Create a graph request asking for user information
        return new Promise((resolve, reject) => {
            const infoRequest = new GraphRequest('/me', {
                    accessToken: accessData.accessToken,
                    parameters: {
                        fields: {
                            string: fields
                        }
                    }
                },
                (error, result) => {
                    if (error) {
                        console.log('Error fetching data: ' + error.toString());
                        reject(error);
                    } else {
                        resolve(result);
                    }
                });

            new GraphRequestManager().addRequest(infoRequest).start();
        });
    };



  triggerGraphRequest = async () => {
            let result = await this.FBGraphRequest('id, email');
            return result;
  }
Run Code Online (Sandbox Code Playgroud)

效果很好!我让您根据您的系统调整我的解决方案。