英文:
How to set body in request to update record using RTK query?
问题
在EditEmployee
组件中,在编辑后,当我点击"保存更改"按钮时,updateEmployee
方法出现了某种原因不能发送PUT
请求的请求体。
我在这里做错了什么?
EditEmployee.js
import React, { useState } from 'react';
import { useUpdateEmployeeMutation } from "./employeesApiSlice";
function EditEmployee(props) {
const [updateEmployee] = useUpdateEmployeeMutation();
const [updatedFirstName, setUpdatedFirstName] = useState(props.employee.firstName);
const [updatedLastName, setUpdatedLastName] = useState(props.employee.lastName);
const [updatedPosition, setUpdatedPosition] = useState(props.employee.position);
const jobPositions = ['Labor', 'Worker', 'Supervisor', 'Manager', 'HR', 'Director', 'CEO']
.map((jobPosition, i) => (
<Col key={i}>
<Form.Group className="mb-3" controlId={jobPosition.toLowerCase()}>
<Form.Check
type="radio"
name="position"
label={jobPosition}
value={jobPosition.toLowerCase()}
defaultChecked={props.employee.position.toLowerCase() === jobPosition.toLowerCase()}
onChange={e => setUpdatedPosition(e.target.value)}
/>
</Form.Group>
</Col>
));
function handleSaveChanges() {
const employee = {
firstName: updatedFirstName,
lastName: updatedLastName,
position: updatedPosition
}
updateEmployee(props.employee.id, employee)
}
return (
<>
<Form>
<Form.Group className="mb-3" controlId="formFirstName">
<Form.Label>First Name</Form.Label>
<Form.Control type="text" defaultValue={updatedFirstName} onChange={e => setUpdatedFirstName(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3" controlId="formLastName">
<Form.Label>Last Name</Form.Label>
<Form.Control type="text" defaultValue={updatedLastName} onChange={e => setUpdatedLastName(e.target.value)} />
</Form.Group>
<Container>
<Row>
{jobPositions}
</Row>
</Container>
</Form>
<Button variant="success" onClick={() => handleSaveChanges()}>
Save Changes
</Button>
</>
);
}
export default EditEmployee;
employeeApiSlice.js
import { apiSlice } from "../../app/api/apiSlice";
export const employeesApiSlice = apiSlice.injectEndpoints({
tagTypes: ['Employees'],
endpoints: (builder) => ({
getEmployees: builder.query({
query: () => ({
url:'/employee'
}),
providesTags:['Employees'],
keepUnusedDataFor: 2
}),
updateEmployee: builder.mutation({
query: (id, employee) => ({
url: `/employee/${id}`,
method: 'PUT',
body: employee
}),
invalidatesTags: ['Employees']
}),
addEmployee: builder.mutation({
query: (employee) => ({
url: `/employee`,
method: 'POST',
body: employee
}),
invalidatesTags: ['Employees']
})
}),
});
export const {
useGetEmployeesQuery,
useUpdateEmployeeMutation,
useAddEmployeeMutation
} = employeesApiSlice;
希望这对你有所帮助。
英文:
In EditEmployee
component after editing when I click on "Save Changes" button the updateEmployee
method for some reason does not send body in PUT
request.
What am I doing wrong here?
EditEmployee.js
import React, { useState } from 'react';
import { useUpdateEmployeeMutation } from "./employeesApiSlice";
function EditEmployee(props) {
const [updateEmployee] = useUpdateEmployeeMutation();
const [updatedFirstName, setUpdatedFirstName] = useState(props.employee.firstName);
const [updatedLastName, setUpdatedLastName] = useState(props.employee.lastName);
const [updatedPosition, setUpdatedPosition] = useState(props.employee.position);
const jobPositions = ['Labor', 'Worker', 'Supervisor', 'Manager', 'HR', 'Director', 'CEO']
.map((jobPosition, i) => (
<Col key={i}>
<Form.Group className="mb-3" controlId={jobPosition.toLowerCase()}>
<Form.Check
type="radio"
name="position"
label={jobPosition}
value={jobPosition.toLowerCase()}
defaultChecked={props.employee.position.toLowerCase() === jobPosition.toLowerCase()}
onChange={e => setUpdatedPosition(e.target.value)}
/>
</Form.Group>
</Col>
));
function handleSaveChanges() {
const employee = {
firstName: updatedFirstName,
lastName: updatedLastName,
position: updatedPosition
}
updateEmployee(props.employee.id, employee)
}
return (
<>
<Form>
<Form.Group className="mb-3" controlId="formFirstName">
<Form.Label>First Name</Form.Label>
<Form.Control type="text" defaultValue={updatedFirstName} onChange={e => setUpdatedFirstName(e.target.value)} />
</Form.Group>
<Form.Group className="mb-3" controlId="formLastName">
<Form.Label>Last Name</Form.Label>
<Form.Control type="text" defaultValue={updatedLastName} onChange={e => setUpdatedLastName(e.target.value)} />
</Form.Group>
<Container>
<Row>
{jobPositions}
</Row>
</Container>
</Form>
<Button variant="success" onClick={() => handleSaveChanges()}>
Save Changes
</Button>
</>
);
}
export default EditEmployee;
employeeApiSlice.js
import { apiSlice } from "../../app/api/apiSlice";
export const employeesApiSlice = apiSlice.injectEndpoints({
tagTypes: ['Employees'],
endpoints: (builder) => ({
getEmployees: builder.query({
query: () => ({
url:'/employee'
}),
providesTags:['Employees'],
keepUnusedDataFor: 2
}),
updateEmployee: builder.mutation({
query: (id, employee) => ({
url: `/employee/${id}`,
method: 'PUT',
body: employee
}),
invalidatesTags: ['Employees']
}),
addEmployee: builder.mutation({
query: (employee) => ({
url: `/employee`,
method: 'POST',
body: employee
}),
invalidatesTags: ['Employees']
})
}),
});
export const {
useGetEmployeesQuery,
useUpdateEmployeeMutation,
useAddEmployeeMutation
} = employeesApiSlice;
答案1
得分: 1
"query"端点的query
回调接受一个单一参数。
参见:Mutation端点定义
如果需要传递多个值,它们应该被打包在一个对象中。
示例:
updateEmployee: builder.mutation({
query: ({ id, employee }) => ({
url: `/employee/${id}`,
method: 'PUT',
body: employee
}),
invalidatesTags: ['Employees']
}),
function handleSaveChanges() {
const employee = {
firstName: updatedFirstName,
lastName: updatedLastName,
position: updatedPosition
};
updateEmployee({ id: props.employee.id, employee });
}
英文:
The query
callback of an endpoint takes a single argument.
See: Mutation Endpoint Definition
> export type MutationDefinition<
> QueryArg,
> BaseQuery extends BaseQueryFn,
> TagTypes extends string,
> ResultType,
> ReducerPath extends string = string,
> Context = Record<string, any>
> > = {
> query(arg: QueryArg): BaseQueryArg<BaseQuery> <---- Single Argument
>
> /* either query
or queryFn
can be present, but not both simultaneously /
> queryFn(
> arg: QueryArg,
> api: BaseQueryApi,
> extraOptions: BaseQueryExtraOptions<BaseQuery>,
> baseQuery: (arg: Parameters<BaseQuery>[0]) => ReturnType<BaseQuery>
> ): MaybePromise<QueryReturnValue<ResultType, BaseQueryError<BaseQuery>>>
>
> / transformResponse only available with query
, not queryFn
/
> transformResponse?(
> baseQueryReturnValue: BaseQueryResult<BaseQuery>,
> meta: BaseQueryMeta<BaseQuery>,
> arg: QueryArg
> ): ResultType | Promise<ResultType>
>
> / transformErrorResponse only available with query
, not queryFn
*/
> transformErrorResponse?(
> baseQueryReturnValue: BaseQueryError<BaseQuery>,
> meta: BaseQueryMeta<BaseQuery>,
> arg: QueryArg
> ): unknown
>
> extraOptions?: BaseQueryExtraOptions<BaseQuery>
>
> invalidatesTags?: ResultDescription<TagTypes, ResultType, QueryArg>
>
> onQueryStarted?(
> arg: QueryArg,
> {
> dispatch,
> getState,
> extra,
> requestId,
> queryFulfilled,
> getCacheEntry,
> }: MutationLifecycleApi
> ): Promise<void>
>
> onCacheEntryAdded?(
> arg: QueryArg,
> {
> dispatch,
> getState,
> extra,
> requestId,
> cacheEntryRemoved,
> cacheDataLoaded,
> getCacheEntry,
> }: MutationCacheLifecycleApi
> ): Promise<void>
> }
If you need to pass multiple values they should be packed in an object.
Example:
updateEmployee: builder.mutation({
query: ({ id, employee }) => ({
url: `/employee/${id}`,
method: 'PUT',
body: employee
}),
invalidatesTags: ['Employees']
}),
function handleSaveChanges() {
const employee = {
firstName: updatedFirstName,
lastName: updatedLastName,
position: updatedPosition
};
updateEmployee({ id: props.employee.id, employee });
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论