0

I'm getting an error(in the android emulator) as

Type error: cannot read property 'clientId' of undefined

When I invoke the function linkedinLogin() then this error is popping up in the alert box.

`clientId = 'xxxxx';`
    linkedinLogin() {
        this.platform.ready().then(() => {
          this.linkedinPage().then(success => {
            alert(success.access_token);
          },(error) => {
            alert(error);
          });
        });
      }

      linkedinPage(): Promise<any> {
        return new Promise(function(resolve, reject) {
          var browserRef = window.cordova.InAppBrowser.open('https://www.linkedin.com/uas/oauth2/authorization?client_id=' + this.clientId + '&redirect_uri=' + this.redirect_uri + '&scope=' + this.appScope.join(" ") + '&response_type=code&state=' + this.state, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');
          browserRef.addEventListener("loadstart", (event) => {
            if((event.url).indexOf(this.redirect_uri) === 0) {
              try {
                var requestToken = (event.url).split("code=")[1].split("&")[0];
                let headers = new Headers();
                headers.append('Content-Type', 'application/x-www-form-urlencoded');
                this.http.post("https://www.linkedin.com/oauth/v2/accessToken?client_id='xxxx'&client_secret='xxxxx'&redirect_uri=http://localhost/callback&grant_type=authorization_code&code=" + requestToken,
                {headers: headers})
                .success(function(data){
                  resolve(data);
                  alert(data);
                })
                .error(function(data,status) {
                  reject('problem in authenticating');
                })
                .finally(function() {
                  browserRef.close();
                },10);
              } catch(e) {
                  setTimeout(function() {
                    browserRef.close();
                  }, 10);
                }
            }
            else {
              browserRef.addEventListener("exit", function(event) {
                reject("The linkedin sign in flow was canceled");
            });
            } 

          });


      });
      }

1 Answers1

0

Set the this keyword inside an variable to access the context of your class like below

    linkedinPage(): Promise<any> {
var self=this;
        return new Promise(function(resolve, reject) {
          var browserRef = window.cordova.InAppBrowser.open('https://www.linkedin.com/uas/oauth2/authorization?client_id=' + self.clientId + '&redirect_uri=' + this.redirect_uri + '&scope=' + this.appScope.join(" ") + '&response_type=code&state=' + this.state, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');

var self=this;

self.clientId

hope this helps

Jameel Moideen
  • 7,542
  • 12
  • 51
  • 79