我正在尝试提交我的个人资料表单并将其添加到我的服务器。我正在使用 RTK Query,并创建了一个 profileSlice.js ...
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
export const profileApiSlice = createApi({
reducerPath: 'profilesApi',
baseQuery: fetchBaseQuery({
baseUrl:"https://intense-mountain-41648.herokuapp.com/api",
}),
endpoints: (build) => ({
fetchProfiles: build.query({
query: () => ({ url:'/profiles' }),
}),
addProfile: build.mutation({
query: (body) => ({
url:"/profiles",
method: 'POST',
body,
}),
})
})
})
export const { useFetchProfilesQuery, useAddProfileMutation } = profileApiSlice;
我还将 addProfile 操作添加到我的商店...
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit';
import currentUserSlice from '../features/currentUser/currentUserSlice';
import { showsApiSlice } from '../api/services/shows-api-slice';
import { userAPISlice } from '../features/userAuth/userSlice';
import { profileApiSlice } from '../features/profiles/profileSlice';
import { setupListeners } from '@reduxjs/toolkit/query'
// can pass in all reducers in this object
export const store = configureStore({
// key names will define the state
reducer: {
currentUser: currentUserSlice,
[showsApiSlice.reducerPath]: showsApiSlice.reducer,
[userAPISlice.reducerPath]: userAPISlice.reducer,
[profileApiSlice.reducerPath]: profileApiSlice.reducer,
},
middleware: (getDefaultMiddleware) => {
return getDefaultMiddleware().concat(showsApiSlice.middleware, userAPISlice.middleware, profileApiSlice.middleware);
}
})
setupListeners(store.dispatch)
一切似乎都在我的前端工作。我的反应钩子正在拾取文本并设置新状态。但是,当我提交表单时,它并没有将它添加到我的数据库中。