简体   繁体   中英

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

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: 在此处输入图像描述

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))
          }


        }
      })
  })

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM