我正在尝试发送之前从 JWT token 解码的用户 ID(连同用户输入的数据)。不幸的是,在我尝试发送它的那一刻,我在后端收到一个异常,说请求数组中的数据为空:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'Approver1lvl', table 'Requests'; column does not allow nulls. INSERT fails.
根据我在浏览器中看到的控制台日志信息判断, token 已正确解码。因此,这让我认为 customInstance.post 部分存在错误。
这是我的代码:
export default function NewRequest() {
const [userData, setUserData] = useState({
firstName: "",
surname: "",
co: "",
homeAddress: "",
postCode: "",
city: "",
country: "",
phonePrefix: "",
phoneNumber: "",
email: "",
approver1lvl: "",
employeeId: "",
userId: ""
});
const handleSubmit = () => {
var token = localStorage.getItem("msal.idtoken");
var jwt = jwt_decode(token);
const {
firstName,
surname,
co,
homeAddress,
postCode,
city,
country,
phonePrefix,
phoneNumber,
email,
approver1lvl,
employeeId,
date,
} = userData;
const user = {
firstName,
surname,
co,
homeAddress,
postCode,
city,
country,
phonePrefix,
phoneNumber,
email,
approver1lvl,
employeeId,
birth,
};
customInstance.post("Request", {user, userId: jwt.oid}).then((response) => {});
};
有人知道怎么解决吗?
最佳答案
我在 Swagger 中看到 userId 与 email, phone ... 处于同一级别,我认为它应该是这样工作的:
export default function NewRequest() {
const [userData, setUserData] = useState({
firstName: "",
surname: "",
co: "",
homeAddress: "",
postCode: "",
city: "",
country: "",
phonePrefix: "",
phoneNumber: "",
email: "",
approver1lvl: "",
employeeId: "",
userId: ""
});
const handleSubmit = () => {
var token = localStorage.getItem("msal.idtoken");
var jwt = jwt_decode(token);
const {
firstName,
surname,
co,
homeAddress,
postCode,
city,
country,
phonePrefix,
phoneNumber,
email,
approver1lvl,
employeeId,
date,
} = userData;
const user = {
firstName,
surname,
co,
homeAddress,
postCode,
city,
country,
phonePrefix,
phoneNumber,
email,
approver1lvl,
employeeId,
birth,
};
customInstance.post("Request", {...user, userId: jwt.oid}).then((response) => {});
};
关于javascript - 使用 Axios 将 MSAL token 发送到后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69966770/