代码之家  ›  专栏  ›  技术社区  ›  MontyGoldy

react/redux:未按预期状态保存

  •  1
  • MontyGoldy  · 技术社区  · 6 年前

    我正在开发一个评论应用程序。我使用json服务器来完成post请求。我的数据库是这样的

    [{
        "id": 5,
        "name": "Tom Cruise",
        "image": "http://placeholder.pics/svg/300x200/000000",
        "title": "Hiring Manager",
        "employeeId": "22222222",
        "funFact": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
        "dateOfJoining": "04/12/2013"
      },
      {
        "id": 6,
        "name": "Julius Caesar",
        "image": "http://placeholder.pics/svg/300x200/fdfdfd",
        "title": "Sales Executive",
        "employeeId": "33333333",
        "funFact": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
        "dateOfJoining": "04/12/2016"
      }
    ]

    这是我的添加操作

    //Add Employee
    export const addEmployee = employeeData => dispatch => {
      axios
        .post("http://localhost:3004/employees", {
          employeeData,
          headers: { "Content-Type": "application/json" }
        })
        .then(response =>
          dispatch({
            type: ADD_EMPLOYEE,
            payload: response.data.employeeData
          })
        )
        .catch(err => console.log(err));
    };

    这是我的减速机

    case ADD_EMPLOYEE:
      return {
        ...state,
        employees: [action.payload, ...state.employees],
        loading: false
      };

    一切正常。问题是它在数据库中的存储方式如下

    {
      "employeeData": {
        "name": "Manpreet Sandhu",
        "title": "bootstrap 4",
        "funFact": "cccZCZCZ",
        "image": "CZCZCZC",
        "employeeId": "ZCZCZC",
        "dateOfJoining": "C"
      },
      "headers": {
        "Content-Type": "application/json"
      },
      "id": 7
    }

    我想删除每个条目前添加的“employeedata”。

    这是我在行动中得到的 enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   kkpoon    6 年前

    参照 axios document ,

    • axios.post(url[, data[, config]])

    在你的行动中,

    //...
    axios
        .post("http://localhost:3004/employees", {
          employeeData,
          headers: { "Content-Type": "application/json" }
        })
    //...
    

    你已经把 headers: { "Content-Type": "application/json" } data 论点

    我想,你应该这样做

    //...
    axios
      .post("http://localhost:3004/employees",
        employeeData, //data
        { headers: { "Content-Type": "application/json" } } //config
      )
    
    // or
    axios({
      method: 'post',
      url: 'http://localhost:3004/employees',
      data: employeeData,
      headers: { "Content-Type": "application/json" }
    })
    //...