uncaught syntaxerror:“ [对象对象]”在json.parse(<anonymous>)

问题描述 投票:0回答:3
代码
const Home = () => { const scrollRef = useRef(null); const userInfo = localStorage.getItem('user') !== 'undefined' ? JSON.parse(localStorage.getItem('user')) : localStorage.clear(); useEffect(() => { const query = userQuery(userInfo?.sub); client.fetch(query).then((data) => { setUser(data[0]); }) }, []); useEffect(()=>{ scrollRef.current.scrollTo(0, 0); }, [])

当您将数据存储在本地存储中时,而不是直接存储数据。

像您一样,从基本上毫无用处的the the clefer cluter。请记住,LocalStorage是字典,但仅存储字符串而不是对象。正如@Naman Agrawal指出的那样,将数据转换为字符串,然后将其存储到localstorage
reactjs syntax-error jsonparser react-google-login
3个回答
4
投票
JSON.stringify(data)


为了更准确地本地化错误,需要更多信息。 如果我们假设在

1
投票
期间发生错误,那是因为在写作过程中使用了对象。 本地存储添加对象

localStorage.getItem('user')

localstorage['user'] = JSON.stringify(data)
(满足我们的订单)

JSON.parse(localStorage.getItem('user'))
//输出👉:[对象对象]

0
投票
//输出👎“ [对象对象]”不是有效的json

Repair:


localStorage.setItem('user', data)

或:


localStorage.getItem(data)

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.