Why does my variable turn to undefined all of sudden, when setting variable from parameter

Viewed 32

I am having problems with this code and can't figure out what is the problem. My idea is to first receive a parameter "bet" and check if the balance is enough, if it is user can start the game and play until he loses. Then when the player finally loses the server receives the "score" and the player is charged. The charging needs to happen after the score is set. But anyway the problem I have is that when the "score" is received and I try to access the recently set variable "bet" it returns undefined?

router.get('/:par',(req,res) =>{

const parameter = JSON.parse(req.params.par)
const bet = parameter.bet


    if(parameter.bet != null){
        //check balance
        console.log(bet)

        res.send("StartTheGame")
    }
    else if(parameter.Score != null){
    console.log(bet)
    //returns undefined?
    }

})

My guess is that when the Score is sent, there is no bet parameter and it sets it to undefined but I don't know how to fix it. Also new to javascript so forgive me if this is not the way to do this.

0 Answers
Related