React Native方法未定义

时间:2018-11-19 12:56:27

标签: javascript react-native redux react-native-fbsdk

我正在尝试在 AccessToken.getCurrentAccessToken()中调用 signInWithFacebook 方法,但是获取 signInWithFacebook 是不确定的。如果我在 LoginManager.logInWithReadPermissions 函数之外调用该方法,则该方法可以正常工作。我似乎无法在 LoginManager.logInWithReadPermissions

中调用任何方法

我是新来的本地人,请帮助。谢谢。

const FBSDK = require('react-native-fbsdk');
const {
  LoginManager,
  AccessToken,
} = FBSDK;
import React from 'react';
import { Text, View, TouchableOpacity, Image } from 'react-native';

import { connect } from 'react-redux';

import { firebaseActions as auth } from "../actions";
const { signInWithFacebook } = auth;



class FacebookLogin extends React.Component {
  constructor() {
    super();

    this.onSuccess = this.onSuccess.bind(this);
    this.onError = this.onError.bind(this);
    this.onSignInWithFacebook = this.onSignInWithFacebook.bind(this);
  }

  async onSignInWithFacebook() {

    LoginManager.logInWithReadPermissions(["public_profile"]).then(
      function (result) {
        if (result.isCancelled) {
          console.log("Login was Cancelled");
        } else {

          AccessToken.getCurrentAccessToken().then(
            (data) => {
              this.props.signInWithFacebook(data.accessToken, this.onSuccess, this.onError);
            }
          );
        }
      },
      function (error) {
        console.log("An error has occured" + error);
      }
    );

  }

  onSuccess({ exists, user }) {
    console.log(
      "Login Success"
    );

  }

  onError(error) {
    console.log(error.message);
  }

  render() {
    return (
      <View>
        <TouchableOpacity
          onPress={this.onSignInWithFacebook}
        >
          <Text>Login</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

export default connect(null, { signInWithFacebook })(FacebookLogin);

2 个答案:

答案 0 :(得分:1)

为此,只需在代码中添加一行作为:-

async onSignInWithFacebook() {
    const _this = this;
    LoginManager.logInWithReadPermissions(["public_profile"]).then(
      function (result) {
        if (result.isCancelled) {
          console.log("Login was Cancelled");
        } else {

          AccessToken.getCurrentAccessToken().then(
            (data) => {
              _this.props.signInWithFacebook(data.accessToken, this.onSuccess, this.onError);
            }
          );
        }
      },
      function (error) {
        console.log("An error has occured" + error);
      }
    );

  }

在您的代码LoginManager中发出一个事件,因此您的范围已更改,并且您获得了未定义的signInWithFacebook。 笔记: - 使用addEventListener()附加函数时,其值会更改...

答案 1 :(得分:1)

在最新版本中发生了变化。

logInWithReadPermissions react-native-fbsdk版本0.8.0中的一种方法。

logInWithPermissions应该使用,因为react-native-fbsdk版本是0.10.0。