yooouuri
yooouuri

Reputation: 2658

Use this in promise

I want to access this in the second then methode from the fetch.

But it keeps saying: TypeError: _this2.login is not a function

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

Upvotes: 0

Views: 67

Answers (1)

robertklep
robertklep

Reputation: 203231

Assuming that this should be the same this that also has the fbAuth method, you should use an arrow function declaration for the first promise as well:

LoginManager.logInWithReadPermissions(['public_profile']).then(res => {
...
});

Upvotes: 4

Related Questions