0

I have changed my CRA react APP into TypeScript and I have a problem below. I think sessionStorage.getItem('accesstoken') should be undefined or string or null and I don't know how to type this object.

 axios
        .post(
          `${process.env.REACT_APP_SERVER_URL}/pick`,
          {
            festivalId: newPick.festivalId,
          },
          {
            headers: {
              accesstoken: sessionStorage.getItem('accesstoken'),
            },
          }
        )

enter image description here

2
  • 1
    You will have to check it the .getItem returns null. You can either pull it out into a variable and check, or use the nullish coalescing operator to give a default accesstoken: sessionStorage.getItem('accesstoken') ?? '', or whatever value you want to give if there's nothing stored. Commented Dec 8, 2022 at 12:19
  • Thanks!! Actually I don't know well about nullish coalescing operator but, as soon as I put ?? '' in my code, it works!! Commented Dec 8, 2022 at 12:23

1 Answer 1

1

It's because null is not accepted as request header value, so you can check if it's exist send if not set undefiend,so it won't send accessToken if it's null

like this :

 headers: {
              accesstoken: sessionStorage.getItem('accesstoken') ?? undefiend,
            },
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.