uniapp報錯記錄:Unexpected end of JSON input、
阿新 • • 發佈:2020-12-29
1、Unexpected end of JSON input原因及如何解決
總是遇到一個報錯,導致有時候渲染有問題,我們看看啥報錯:
11:29:07.092 [Vue warn]: Error in render: "SyntaxError: Unexpected end of JSON input"
11:29:07.131 (found at pages/account/account.vue:1)
11:29:07.152 SyntaxError: Unexpected end of JSON input
初看是JSON解析的問題。但是account程式碼很簡單,沒什麼json解析問題啊,後來發現是取userInfo資訊時報錯
getters: {
userInfo(state) {
if (!state.userInfo) {
state.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
}
return state.userInfo
}
},
如果userInfo不存在時,去storage裡面取,如果不存在,這時候就為空。JSON.parse(''),所以就報這個錯。
JSON.parse('')
// VM483:1 Uncaught SyntaxError: Unexpected end of JSON input
// at Object.parse (<anonymous>)
// at <anonymous>:1:6
那麼知道了原因,稍微改一下即可:
getters: {
userInfo(state) {
if (!state.userInfo) {
let _user = uni.getStorageSync('userInfo')
state.userInfo = _user ? JSON.parse(_user) : null
}
return state.userInfo
}
},