How to set condition if APi response is null then do the following VUEjs?

Viewed 309

I have a method where I get token from API:

 let { Token } = await API.getToken({
       postId: postId
      })
if(){}

When token does not exist I see in the console that it's null warning. "Cannot read property 'Token' of null" Because API response itself is null. My goal is that if it shows that warning and it's null then use the logic inside if condition. I tried to use if(token==null) but it didn't work. How can be it done?

1 Answers

Get the response without destructing it :

   let response = await API.getToken({
   postId: postId
  })

 if(response===null){
 
  }else{
   //use response.Token
 }
Related