我从我的axios post请求中得到这个错误错误消息:{error: "Cannot read properties of undefined (reading ' apartment ')"}而发送的数据正是应该发送的…我用相同的数据测试api,它工作,但客户端不工作。
这是我的请求:
async function postData() {
const requestData = {
User: "642ab5c99f9ad0ae266a8391",
appartment: "642187183d7a4584d959d063",
description: "my reservation",
checkIn: "2023-04-12",
checkOut: "2023-04-24",
servicesFee: 150,
nightsFee: 650,
services: ["641c363db26efcbc26b02faa", "641c37ee8478c385e59bf27a"],
};
console.log("Request data:", requestData);
try {
const response = await axios.post(
"http://localhost:9090/user/reservations/createOrder",
requestData,
{
headers: {
Authorization: `Bearer ${user.token}`,
"Content-Type": "application/json",
},
}
);
console.log("Response data:", response.data);
// Handle success
} catch (error) {
console.error("Error message:", error.response.data);
// Handle error
}
}
,这是邮递员发送到相同端点的正文:
{
"Order": {
"User" : "642ab5c99f9ad0ae266a8391",
"appartment":"642187183d7a4584d959d063",
"description": "my reservation ",
"checkIn": "2023-04-12",
"checkOut": "2023-04-24",
"servicesFee": 150,
"nightsFee": 650,
"services": [
"641c363db26efcbc26b02faa",
"641c37ee8478c385e59bf27a"
]
}
}
我敢肯定这不是一个打字错误
我认为你应该把requestData放到Order对象。与邮递员发送的主体相同
const requestData = {
Order : {
User: "642ab5c99f9ad0ae266a8391",
appartment: "642187183d7a4584d959d063",
description: "my reservation",
checkIn: "2023-04-12",
checkOut: "2023-04-24",
servicesFee: 150,
nightsFee: 650,
services: ["641c363db26efcbc26b02faa", "641c37ee8478c385e59bf27a"],
}
};