在承诺中使用此

时间:2017-09-30 10:17:08

标签: javascript react-native promise

我想在第二个访问this然后从fetch中访问。

但它一直说:TypeError:_this2.login不是函数

  fbAuth() {
    LoginManager.logInWithReadPermissions(['public_profile']).then(function(res) {
      if (res.isCancelled) {
        console.info('cancelled');
      } else {
        console.info('login success');

        AccessToken.getCurrentAccessToken().then((data) => {
          let req = new GraphRequest('/me', {
            httpMethod: 'GET',
            version: 'v2.5',
            parameters: {
              'fields': {
                'string' : 'id,email,name'
              }
            }
          }, (err, res) => {
            const data = new FormData();
            data.append('facebook_user_id', res.id);
            data.append('email', res.email);
            data.append('name', res.name);

            fetch('xxxxxxxxxxx', {
              method: 'POST',
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              body: data,
            })
              .then((response) => response.json())
              .then((responseJson) => {
                this.login(responseJson);
              })
              .catch((error) => {
                console.error(error);

                alert('Something went wrong. Please try again, if the problem persists contact the @lacarte service desk');
              });
          });

          new GraphRequestManager().addRequest(req).start();
        });
      }
    }, function(error) {
      console.error(error);
    });
  }

1 个答案:

答案 0 :(得分:4)

假设this应该是this同样具有fbAuth方法的LoginManager.logInWithReadPermissions(['public_profile']).then(res => { ... }); ,您还应该为第一个承诺使用箭头函数声明:

public class App implements Serializable {
     public static void main(String[] args) {
         SparkSession sparkSession = SparkSession.builder()
                 .appName("SparkSQL")
                 .master("local")
                 .getOrCreate();
         Dataset<Row> df = sparkSession.createDataFrame(
                 Arrays.asList(new Person("panfei",27)),Person.class);
         System.out.println(df.rdd().count());
     }
}