1

The following gets the URL but can't convert it to string. respJSON.mySite has the value but not mySiteFullURL. I tried multiple approaches like tostring(), etc. but still can't get the URL as string.

protected _readMySite():string {
let mySiteFullURL : string;
this.context.graphHttpClient.get(`v1.0/me?$select=mySite`, GraphHttpClient.configurations.v1).then((response: HttpClientResponse) => {
  if (response.ok) {
    return response.json();
  } else {
    console.warn(response.statusText);
  }
}).then((respJSON:any) => {
  console.log('respJSON.mySite ' + respJSON.mySite);
  mySiteFullURL = respJSON.mySite;
});
console.log('mySiteFullURL '+mySiteFullURL);
return mySiteFullURL;

}

0

You need to return a Promise which will give you the value. You are currently returning a string which will undefined since your method will not wait for the ajax call to be completed.

Modify your code as below:

protected _readMySite(): Promise<any> {
let mySiteFullURL: string;
    return this.context.graphHttpClient.get(`v1.0/me?$select=mySite`, GraphHttpClient.configurations.v1).then((response: HttpClientResponse) => {
      if (response.ok) {
        return response.json();
      } else {
        console.warn(response.statusText);
      }
    }).then((respJSON: any) => {
      console.log('respJSON.mySite ' + respJSON.mySite);
      mySiteFullURL = respJSON.mySite;
      return mySiteFullURL;
});

Usage:

var myPersonalsite = this._readMySite();
myPersonalsite.then(function(result){
  console.log(result);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.