【问题标题】:Using promise with GraphRequestManager将 promise 与 GraphRequestManager 一起使用
【发布时间】:2016-09-25 22:58:48
【问题描述】:

有没有人举例说明如何将 Promise 与 GraphRequestManager 一起使用? 我在我的动作创建器中收到无法读取属性然后出现未定义错误。

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();
});

}

我使用我的动作创建器调用上述内容

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

}

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

【问题讨论】:

    标签: react-native react-redux es6-promise react-native-fbsdk


    【解决方案1】:

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

     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;
      }
    

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

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2017-09-03
      • 2018-03-02
      • 2015-08-03
      相关资源
      最近更新 更多