当前位置 : 主页 > 网页制作 > React >

react-native – 使用GraphRequestManager的promise

来源:互联网 收集:自由互联 发布时间:2021-06-15
有没有人有关于如何使用GraphRequestManager的promise的例子? 我得到了无法在我的动作创建者中读取未定义错误的属性. function graphRequest(path, params, token=undefined, version=undefined, method='GET') {r
有没有人有关于如何使用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();
});

}

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

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()中的帐户之后.然后((帐户)我认为是由于GraphRequestManager立即返回而不是等待.

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

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

这很棒!我让你的解决方案适应你的系统.

网友评论