所以我创建一个crud应用程序使用strapi rest api作为我的后端,但我是一个新的React开发人员,我似乎不能处理put请求更新api响应。它在postman中成功工作,但在应用程序中抛出400错误请求
这是rtk突变减少器的终点
updatePet: builder.mutation({
query: (payload) => ({
url: `/pets/${payload.id}`,
method: 'PUT',
body: payload,
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
}),
invalidatesTags: ['Pet'],
}),
editPetEntry页面
import React, { useState, useEffect } from 'react';
// mui components
import {
Typography,
TextField,
Box,
Button,
Paper
} from '@mui/material';
// mui icons
import { Edit } from '@mui/icons-material';
// custom components
import BottomNav from './BottomNav';
import { useUpdatePetMutation, useGetPetQuery } from '../services/petApi';
import Loader from './Loader';
import { useParams } from 'react-router-dom';
import { usePetContext } from '../petContext';
export default function EditPetEntry() {
// input data
const [name, setName] = useState("");
const [animal, setAnimal] = useState("");
const [breed, setBreed] = useState("");
const [age, setAge] = useState("");
const [location, setLocation] = useState("");
const [sex, setSex] = useState("");
// const {petId} = useParams();
const { petId } = useParams();
// edit req
const { data: pet} = useGetPetQuery(petId)
const [updatePet, { isLoading }] = useUpdatePetMutation();
const data = JSON.stringify({
"data": {
"name": name,
"animal": animal,
"breed": breed,
"age": age,
"location": location,
"sex": sex
}
});
const handleEditPet = () => {
updatePet({ id: petId, data });
};
console.log(data);
console.log(petId);
console.log(pet);
return (
<Box
component="form"
sx={{
'& .MuiTextField-root': { m: 1, width: '50ch' },
display: 'flex',
flexDirection: 'column'
}}
noValidate
autoComplete="off"
>
<div>
<Typography variant="h3" gutterBottom component="div">
Edit Pet entry
</Typography>
<TextField
required
id="filled-name"
label="Name"
variant="outlined"
onChange={(e) => setName(e.target.value)}
/>
<TextField
required
id="filled-animal"
label="Animal"
variant="outlined"
helperText="Cat, Dog, Bird"
onChange={(e) => setAnimal(e.target.value)}
/>
<TextField
required
id="filled-breed-input"
label="Breed"
variant="outlined"
onChange={(e) => setBreed(e.target.value)}
/>
<TextField
required
id="filled-location-input"
label="Location"
variant="outlined"
onChange={(e) => setLocation(e.target.value)}
/>
<TextField
required
id="filled-age"
label="Age"
type="number"
variant="outlined"
onChange={(e) => setAge(e.target.value)}
/>
<TextField
required
id="sex"
label="Sex"
helperText="Male, Female"
variant="outlined"
onChange={(e) => setSex(e.target.value)}
/>
</div>
<div>
<Button variant="outlined" onClick={handleEditPet} startIcon={<Edit />}>
Edit Pet Entry
</Button>
{isLoading && <Loader />}
</div>
<Paper sx={{ position: 'fixed', bottom: 0, left: 0, right: 0 }} elevation={3}>
<BottomNav />
</Paper>
</Box>
);
}
我一直犯的错误
{"data":null,"error":{"status":400,"name":"ValidationError","message":"Missing \"data\" payload in the request body","details":{}}}
我如何处理它我可以在一个特定的id更新的细节,我从useparams使用React路由器获取petId
1条答案
按热度按时间juzqafwq1#
在updatePet突变中
你传递数据作为有效载荷的原因是,主体应该显式地具有包含所有数据的data属性。
你也不需要
和JSON。stringy,这一切都是在引擎盖下为您完成的