简体   繁体   English

未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> ) at./src/context/AuthContext.js</anonymous>

[英]Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at ./src/context/AuthContext.js

i was developing a booking app using MERN STACK,can you please help me in this error我正在使用 MERN STACK 开发一个预订应用程序,你能帮我解决这个错误吗

So the error comes in this part of Code in AuthContext.js file所以错误出现在 AuthContext.js 文件中的这部分代码中

const INITIAL_STATE = {
  user: JSON.parse(localStorage.getItem("user")) || null,
  loading: false,
  error: null,
}

I used the useEffect我使用了 useEffect

export const AuthContextProvider=({children})=>{
  const [state,dispatch]=useReducer(AuthReducer,INITIAL_STATE);

  useEffect(() => {
    localStorage.setItem("user",JSON.stringify(state.user))
  },[state.user]);

and the error which comes以及随之而来的错误

Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at./src/context/AuthContext.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at./src/context/Auth

localStorage.get returns undefined which cannot be parsed by JSON.parse. localStorage.get返回无法被 JSON.parse 解析的未定义。 I usually use a function like this which catches the error and returns the value itself as a fallback.我通常使用像这样的 function 来捕获错误并将值本身作为后备返回。

function parse(value){
   try {
      return JSON.parse(value)
   } catch(_) {
      return value
   }
}

Error message indicates that issue is within JSON.parse which is used in INITIAL_STATE .错误消息表明问题在INITIAL_STATE中使用的JSON.parse中。

You can add guard, something like this:您可以添加警卫,如下所示:

const userLs = localStorage.getItem("user");
const INITIAL_STATE={
  user: userLs ? JSON.parse(userLs) : null,
  loading:false,
  error:null,
}

You should always remember two things when getting an item from your localStorage and parsing it:localStorage获取项目并对其进行解析时,您应该始终记住两件事:

  1. You should always handle the case that localStorage does not have the item you're trying to get.您应该始终处理localStorage没有您要获取的项目的情况。 If the item is not there, your localStorage.getItem() call will return undefined .如果该项目不存在,您的localStorage.getItem()调用将返回undefined
  2. You should never pass an invalid JSON object into a JSON.parse() call.永远不应将无效的 JSON object 传递给JSON.parse()调用。 undefined is not a valid JSON item. undefined不是有效的 JSON 项目。 So in your case, you should separate out the localStorage.getItem() call and probably store it in a variable, and then, only if you get a valid JSON back, pass the variable into your JSON.parse() call.因此,在您的情况下,您应该将localStorage.getItem()调用分离出来,并可能将其存储在一个变量中,然后,只有当您得到一个有效的 JSON 时,才将该变量传递给您的JSON.parse()调用。

The other answers here have given you example ways of doing this.此处的其他答案为您提供了执行此操作的示例方法。

If you still get the same error after trying the above, then your stored JSON value maybe corrupted.如果您在尝试上述操作后仍然遇到相同的错误,那么您存储的 JSON 值可能已损坏。 It's hard to say why without looking at the actual returned item from the localStorage .如果不查看从localStorage实际返回的项目,很难说为什么。 You could probably delete the item from your localStorage and start the authentication from scratch, to see if that fixes the issue.您可能可以从localStorage中删除该项目并从头开始进行身份验证,以查看是否可以解决问题。

You are actually not passing the details that you want store in the Local Storage.您实际上并没有传递要存储在本地存储中的详细信息。 Please pass the user details as an object(inside curly bracket) in the backend.请在后端将用户详细信息作为对象(在大括号内)传递。

Hint: please reconstruct your data then pass提示:请重建您的数据然后通过

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous> ) - VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse的位置2 <anonymous> )在对象上。 <anonymous> (userhistory.js:23) - Uncaught SyntaxError: Unexpected token < in JSON at position 2 at JSON.parse (<anonymous>) at Object.<anonymous> (userhistory.js:23) Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 : at JSON.parse (<anonymous> ) 在对象。<anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM