0
class User {
    public pname: string;
    private ptoken: string;
    public token_status:boolean = false;

  

    public constructor(pname: string, ptoken: string) {
        this.pname = pname;
        this.ptoken = ptoken;
    }




    public Token_Valid() : Promise<token_format>{

        
        return new Promise((accept:any)=>{

            db.all("select * from `token` where `name` = $name and `token` = $token;",{$name:this.pname,$token:this.ptoken},(err:any,res:any)=>{
                if(res[0]){
                    accept(res[0]);
                }else{
                    accept({Error:'Person was not found, or token is incorrect soryy ...'})
                }
            });

            
        })

      
        this.token_status = true;

       

    }

The Following Message is given when I try to assign the boolean value in the method too true

** Could someone explain the reason the error is happening and also how does global variables work in classes inside Typescript / Javascript as in other languages this wouldnt be a problem <3 **

Milahn
  • 21
  • 3

1 Answers1

1

The problem here seems to be pretty simple, you are returning a new Promise inside the Token_Valid() method, but setting this.token_status after you return it.

In JavaScript, nothing can be done after the return statement of a method*, so you'll want to refactor that method to either:

  1. Set the variable before you return the promise,
  2. Save the value of the promise as a variable by awaiting it, then set this.token_status, and return the previously-saved value.
  3. Add a .then() to the returned promise which takes in a value (the one returned from the created promise) that changes the value of this.token_status and then returns the value it took in.

*unless you use a try { ... } finally { ... } block.

Chris Gilardi
  • 1,509
  • 14
  • 26