Home > Software design >  React LocalStorage issue not stored in localstorage
React LocalStorage issue not stored in localstorage

Time:06-22

I am using Local Storage for my login page but my variables not storing in the local storage I don't know why....

I am using the following code on my button click....

But the APi i am using is correct... It works fine

res.data.status gives true or false,Inside Axios .then => If is used for correct username and password and else is used for incorrct user

This is my Code:

async function handleSubmit(e) {
    var url = 'http://localhost/project/login.php?name=' name "&price=" price;
    const formData = new FormData();
    formData.append('avatar',"hi")
    await axios.post(url, formData)
    .then(res => {
      if(!res.data.status){
        localStorage.setItem('username', name);
       alert(res.data.message);
      }else{
       alert(res.data.message);
      }
    })
}

CodePudding user response:

if your variable is not stored in the localStorage. that's because of the condition you have. also as you're sure that your API is working fine and you can successfully make a request and receive a response. then the issue is with the condition. because from your code. you're making conditions only if the request is not successful. you don't have the condition for success.

async function handleSubmit(e) {
    var url = 'http://localhost/project/login.php?name=' name "&price=" price;
    const formData = new FormData();
    formData.append('avatar',"hi")
    await axios.post(url, formData)
    .then(res => {
      if(!res.data.status){ <= remove the !
        localStorage.setItem('username', name);
       alert(res.data.message);
      }else{
       alert(res.data.message);
      }
    })
}
  • Related