[英]Uncaught (in promise) TypeError: Cannot perform 'set' on a proxy that has been revoked
I am trying to build a login/signup system on my React website using JWT .我正在尝试使用JWT在我的React网站上构建登录/注册系统。
I have an API set up which works perfectly.我有一个完美的 API 设置。 I need to integrate the user login flow with the server.
我需要将用户登录流程与服务器集成。
This is my userSlice
-这是我的
userSlice
-
import { createSlice } from "@reduxjs/toolkit";
export const userSlice = createSlice({
name: "user",
initialState: {
user: null,
},
reducers: {
signup: (state, action) => {
fetch("http://localhost:5000/user/signup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(action.payload),
})
.then((res) => {
console.log(res.data);
state.user = res.data;
})
.catch((e) => {
console.log(e);
});
},
login: (state, action) => {
fetch("http://localhost:5000/user/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(action.payload),
}).then((res) => {
state.user = res.data;
});
},
logout: (state) => {},
},
});
export const { login, logout, signup } = userSlice.actions;
export const selectCount = (state) => state.user.user;
export default userSlice.reducer;
Signup
Component - Signup
组件-
import React, { useState } from "react";
import { useDispatch } from "react-redux";
import { signup } from "../../features/userSlice";
import "./Signup.css";
function Signup() {
const [email, setEmail] = useState("");
const [name, setName] = useState("");
const [password, setPassword] = useState("");
const dispatch = useDispatch();
const signUpHandler = (e) => {
e.preventDefault();
dispatch(signup(name, email, password));
};
return (
<div className="signup">
<h3>Sign Up</h3>
<form method="POST">
<div className="input_area">
<label>Username</label>
<input type="text" onChange={(e) => setName(e.target.value)} />
</div>
<div className="input_area">
<label>Email</label>
<input type="text" onChange={(e) => setEmail(e.target.value)} />
</div>
<div className="input_area">
<label>Password</label>
<input
type="password"
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<button onClick={signUpHandler} type="submit">
Sign Up
</button>
</form>
</div>
);
}
export default Signup;
When I click the Sign Up button, I get this error -当我单击“注册”按钮时,出现此错误 -
Uncaught (in promise) TypeError: Cannot perform 'set' on a proxy that has been revoked未捕获(承诺)类型错误:无法在已撤销的代理上执行“设置”
Any idea what's causing the error?知道是什么导致了错误? The backend works fine.
后端工作正常。
Thanks in advance.提前致谢。
EDIT:编辑:
There was a mistake on my part after fetch.获取后我犯了一个错误。 Instead of
state.user = res.data
I had to do state.user = JSON.parse(res.data)
而不是
state.user = res.data
我必须做state.user = JSON.parse(res.data)
But after I do this, I get a different error which says -但是在我这样做之后,我得到了一个不同的错误,它说 -
Unhandled Rejection (SyntaxError): Unexpected token u in JSON at position 0未处理的拒绝(SyntaxError):JSON 中位置 0 的意外标记 u
This is the response I get in POSTMAN这是我在 POSTMAN 中得到的回应
我认为您可能想改用https://redux-toolkit.js.org/api/createAsyncThunk
因为您在这里调用 api。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.