Home > other >  My local storage is not working, I am trying to push values but I got empty
My local storage is not working, I am trying to push values but I got empty

Time:02-27

I am fetching url with a query if the query link is available on my database it will return true, after true I will set the query link into the local storage but when I try to set local storage I got an empty value. I want to push query links in an array but I got this: enter image description here

Here is my code:

  useLayoutEffect(() => {
    fetch(`http://localhost:5000/findUrl/${affiliateLink}`)
      .then(res => res.json())
      .then(data => {
        if (data.isUrlTrue) {
          const affiliate_link = localStorage.getItem('affiliate_Link')
          if (affiliate_link === null) {
            localStorage.setItem('affiliate_Link', [])
          } else {
            let old_Data = JSON.parse(affiliate_link).push(affiliateLink)
            localStorage.setItem('affiliate_Link', JSON.stringify(old_Data))
          }


        }
      })
  })

CodePudding user response:

Try this

let old_Data = JSON.parse(affiliate_link) || [];
old_Data.push(affiliateLink);
localStorage.setItem('affiliate_Link', JSON.stringify(old_Data));
  • Related