javascript - 使用 Axios 将 MSAL token 发送到后端

标签 javascript reactjs web axios msal

我正在尝试发送之前从 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) => {});
  };

有人知道怎么解决吗?

编辑:浏览器错误截图:enter image description here

EDIT2:我已经使用 swagger 成功发送了一个 POST,它看起来像这样: enter image description here

最佳答案

我在 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/

相关文章:

javascript - JS/JQuery : Value from select OR from text input

javascript - 如何在 Android webview 中通过 ID 捕获元素点击?

javascript - react 显示 block 与悬停信息

ruby-on-rails - Highcharts 不适用于 wicked_pdf

ajax - 百合的 Mathjax 模拟

javascript - 自动创建 Javascript 对象并将其存储在另一个对象中

javascript - 你如何创建一个函数来按需加载图像

javascript - HTML 到呈现的 html

javascript - 获取组件数组的长度

jquery - 当用户将鼠标悬停在图像上时,如何获得图像的缩放效果?