Invalid type "string | null" of template literal expression. while sending authorization

Viewed 3442

Ts-eslint stoped me with the message "Invalid type "string | null" of template literal expression" when i was trying to execute authorization. value came form localstorage so it has to be null or 'string' but also i have to combinde it with Barer. I'm thinking on it whole day and stil nothing. Please help

   onMounted(async ()=>{
      let myToken = localStorage.getItem('token');
      
      await axios.post(
        'http://localhost:3000/getdocuments', 
        {headers:{'Authorisation':`Baerer ${myToken}`}}
      )
      .then((res)=>{
          console.log(res);
      })
      .catch(err=>{
          console.log(err);
      })
    })

1 Answers

I think this request should be called only if token is present

onMounted(async ()=>{
      let myToken = localStorage.getItem('token');
      if (!myToken) {
        console.warn('token is empty');
      } else {
        await axios.post(
          'http://localhost:3000/getdocuments', 
          {headers:{'Authorisation':`Baerer ${myToken}`}}
        )
        .then((res)=>{
            console.log(res);
        })
        .catch(err=>{
            console.log(err);
        })
      }
    })
Related