Next.js Discord

Discord Forum

Class data member state

Unanswered
Yellow Crazy Ant posted this in #help-forum
Open in Discord
Yellow Crazy AntOP
hey can someone help me out with this generic issue I am facing with JS

In the provided code, I'm trying to understand why I see the updated access-token value in console.log("newToken", this.apiConfig.accessToken); during the first call to refreshAccessToken, but the console.log("OldToken___", this.apiConfig.accessToken); still shows the old access-token value during the second call,(Even when I make clearly see the value being updated in the first call to refreshAccessToken)

Here’s the relevant part of the code for reference:

export default class abc {
  private apiConfig: {
    accessToken: string;
    refreshToken: string;
    clientSecret: string;
    clientID: string;
    oauthUrl: string;
    userId: number;
    createdAt: number;
    expiresIn: number;
  };


//constructor here 

private async refreshAccessToken() {
    const data = await this.requestNewAccessToken();


    console.log("oldAccessToken", this.apiConfig.accessToken);
    this.apiConfig = {
        ...this.apiConfig,
        accessToken: data.access_token,
        refreshToken: data.refresh_token,
        createdAt: data.created_at,
        expiresIn: data.expires_in,
    };
    console.log("newAccessToken", this.apiConfig.accessToken);
}

//OTHER methods which call refreshAccessToken

}


Here is what the log shows

//first call 
oldAccessToken az6KQfdP301P9yUzxoEVnVFtWKW5wLdsBdfMxar29iU
newAccessToken CLEdSv-MDZDDiS8Ttxdn6Bs6hpgEvwI4C5d3bj1JPq0


//second call
oldAccessToken az6KQfdP301P9yUzxoEVnVFtWKW5wLdsBdfMxar29iU

4 Replies

@Yellow Crazy Ant hey can someone help me out with this generic issue I am facing with JS In the provided code, I'm trying to understand why I see the updated access-token value in console.log("newToken", this.apiConfig.accessToken); during the first call to refreshAccessToken, but the console.log("OldToken___", this.apiConfig.accessToken); still shows the old access-token value during the second call,(Even when I make clearly see the value being updated in the first call to refreshAccessToken) Here’s the relevant part of the code for reference: export default class abc { private apiConfig: { accessToken: string; refreshToken: string; clientSecret: string; clientID: string; oauthUrl: string; userId: number; createdAt: number; expiresIn: number; }; //constructor here private async refreshAccessToken() { const data = await this.requestNewAccessToken(); console.log("oldAccessToken", this.apiConfig.accessToken); this.apiConfig = { ...this.apiConfig, accessToken: data.access_token, refreshToken: data.refresh_token, createdAt: data.created_at, expiresIn: data.expires_in, }; console.log("newAccessToken", this.apiConfig.accessToken); } //OTHER methods which call refreshAccessToken } Here is what the log shows //first call oldAccessToken az6KQfdP301P9yUzxoEVnVFtWKW5wLdsBdfMxar29iU newAccessToken CLEdSv-MDZDDiS8Ttxdn6Bs6hpgEvwI4C5d3bj1JPq0 //second call oldAccessToken az6KQfdP301P9yUzxoEVnVFtWKW5wLdsBdfMxar29iU
i can't reproduce this.
class Test {
  private apiConfig: {
    token: string;
  };

  constructor() {
    this.apiConfig = { token: "first token" };
  }

  private refreshToken() {
    console.log("Old token: ", this.apiConfig.token);
    this.apiConfig = { ...this.apiConfig, token: "new token" };
    console.log("New token: ", this.apiConfig.token);
  }

  public main() {
    this.refreshToken();
  }
}

const test = new Test();

test.main();
test.main();

Old token:  first token
New token:  new token
Old token:  new token
New token:  new token
@Yellow Crazy Ant hey can someone help me out with this generic issue I am facing with JS In the provided code, I'm trying to understand why I see the updated access-token value in console.log("newToken", this.apiConfig.accessToken); during the first call to refreshAccessToken, but the console.log("OldToken___", this.apiConfig.accessToken); still shows the old access-token value during the second call,(Even when I make clearly see the value being updated in the first call to refreshAccessToken) Here’s the relevant part of the code for reference: export default class abc { private apiConfig: { accessToken: string; refreshToken: string; clientSecret: string; clientID: string; oauthUrl: string; userId: number; createdAt: number; expiresIn: number; }; //constructor here private async refreshAccessToken() { const data = await this.requestNewAccessToken(); console.log("oldAccessToken", this.apiConfig.accessToken); this.apiConfig = { ...this.apiConfig, accessToken: data.access_token, refreshToken: data.refresh_token, createdAt: data.created_at, expiresIn: data.expires_in, }; console.log("newAccessToken", this.apiConfig.accessToken); } //OTHER methods which call refreshAccessToken } Here is what the log shows //first call oldAccessToken az6KQfdP301P9yUzxoEVnVFtWKW5wLdsBdfMxar29iU newAccessToken CLEdSv-MDZDDiS8Ttxdn6Bs6hpgEvwI4C5d3bj1JPq0 //second call oldAccessToken az6KQfdP301P9yUzxoEVnVFtWKW5wLdsBdfMxar29iU
Kurilian Bobtail
Are you sure you are operating on the same instance? Cloud be two different instances of abc
Yellow Crazy AntOP
Yes I am making a single instance and passing it to fetchData function which internally calls different methods of that object

  const abcObject = new ABC({
      accessToken,
      refreshToken,
      clientID,
      clientSecret,
      oauthUrl,
      userId,
      createdAt,
      expiresIn,
    });

    //0. Getting user data from calendly
    const { dataX, dataY } = await fetchData(
      abcObject
    );


Fetch data is something like
const fetchData = async (abcObject)=>{
try{

  const dataX = await abcObject.fetchX({
      //params
    });

    const dataY = await abcObject.fetchY({
      //params
    });

//fetchX and fetchY call the refreshToken internally

return {
      dataX,
      dataY,

    };
}
catch (error) {
    //handle error
  }
}
Kurilian Bobtail
Is there any other place where accessToken might be re-assigned? inside another method maybe