我正在构建一个评论应用。我正在使用json-server来发布请求。我的数据库看起来像这样
[{
"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”。
答案 0 :(得分:1)
请参阅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" }
})
//...