使用React Native在Facebook中创建登录名时,调用AccessToken.getCurrentAccessToken()中的方法

时间:2018-12-11 08:00:58

标签: javascript facebook react-native login

在我的应用中,我正在使用React Native在Facebook中创建一个登录名,这是我的代码:

 async handleFacebookLogin() {
    LoginManager.logInWithReadPermissions(['public_profile', 'email', 'user_friends']).then(
        function (result) {
            if (result.isCancelled) {
                console.log('Login cancelled')
            } else {
                console.log('Login success with permissions: ' + result.grantedPermissions.toString())

                AccessToken.getCurrentAccessToken().then(
                    async (data) => {

                        let resultChild = await loginWithFaceBook(data.accessToken.toString(), "POST");

                        if (resultChild.username.length > 0) {
                            this.loginWithFaceBook(resultChild.token);
                        }
                    });
            }
        },
        function (error) {
            console.log('Login fail with error: ' + error)
        }
    )
}

我的方法:

loginWithFaceBook = async (tokenFace) => {
    Toast.show('Login success!');
    this.saveTokenLogin(tokenFace); 
}

这是我的错误:

_this.loginWithFaceBook is not a function

如何在AccessToken.getCurrentAccessToken()中使用方法?

1 个答案:

答案 0 :(得分:0)

插入此代码:

const _this = this;

到下面的代码:

async handleFacebookLogin() {

并替换此代码:

  this.loginWithFaceBook(resultChild.token);

通过此代码:

_this.loginWithFaceBook(resultChild.token);

享受。

这是完整的代码:

 async handleFacebookLogin() {
    const _this = this;
    LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(
        function (result) {
            if (result.isCancelled) {
                console.log('Login cancelled')
            } else {
                console.log('Login success with permissions: ' + result.grantedPermissions.toString())
                let tokenFace = '';
                AccessToken.getCurrentAccessToken().then(
                    async (data) => {
                        data.accessToken)
                        let resultChild = await loginWithFaceBook(data.accessToken.toString(), "POST");
                        if (resultChild.username.length > 0) {
                            _this.loginFaceBook(resultChild.token);
                        }
                    });
            }
        },
        function (error) {
            console.log('Login fail with error: ' + error)
        }
    )
}