Rumah > Soal Jawab > teks badan
Saya mempunyai borang berbilang langkah dan apabila saya cuba menyerahkan ia menunjukkan ralat ini. Walau bagaimanapun, jika saya menggunakan jadual createOrganization dalam bentuk ringkasnya, ia berfungsi. Adakah ia masalah bahagian belakang atau masalah bahagian hadapan? Apa yang boleh saya lakukan salah? Saya akan menghargai sebarang petua
Onboarding.jsx
import Stepper from "./components/Stepper"; import Account from "./components/steps/Account"; import Details from "./components/steps/Details"; import Payment from "./components/steps/Payment"; import { createOrganization } from "../../../features/organization/organizationSlice"; const Onboarding = () => { const toast = useToast() const [step, setStep] = useState(0); const steps = ["", "", "",]; const navigate = useNavigate(); const dispatch = useDispatch(); const { organization, isError, isSuccess, message } = useSelector( (state) => state.organization ); const [formData, setFormData] = useState({ businessName: "", businessEmail: "", currency: "", industry: "", businessDescription: "", useCase: "", organizationType: "", website: "", businessPhone: "", country: "", billingName: "", billingEmail: "", addressState: "", addressCity: "", addressStreet: "" }); const displayStep = () => { switch (step) { case 0: return <Account formData={formData} setFormData={setFormData} />; case 1: return <Details formData={formData} setFormData={setFormData} />; case 2: return <Payment formData={formData} setFormData={setFormData} />; default: } }; const handleSubmit = () => { dispatch(createOrganization(formData)) } }; return ( <Stepper steps={steps} step={step} /> <div className="my-5 p-10 "> <div>{displayStep()}</div> {step > 0 && <Button onClick={() => setStep(step - 1)}>Back</Button>} <Button onClick={handleSubmit}> { step === 0 || step === 1 ? "Next" : "Submit" } </Button>
Atur Slice.jsx
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; import organizationService from "./organizationService"; const initialState = { organization: [], isLoading: false, isError: false, isSuccess: false, message: "", }; export const createOrganization = createAsyncThunk( "organization/createOrganization", async (organization, thunkAPI) => { try { const token = thunkAPI.getState().auth.user.token; console.log("USER TOKEN", token); return await organizationService.createOrganization(organization, token); } catch (error) { const message = (error.response && error.response.data && error.response.data.message) || error.message || error.toString(); return thunkAPI.rejectWithValue(message); } } ); export const organizationSlice = createSlice({ name: "organization", initialState, extraReducers: (builder) => { builder .addCase(createOrganization.pending, (state) => { state.isLoading = true; state.isError = false; }) .addCase(createOrganization.fulfilled, (state, action) => { state.isLoading = false; state.isSuccess = true; state.organization = action.payload; }) .addCase(createOrganization.rejected, (state, action) => { state.isLoading = false; state.isError = true; state.message = action.payload; }); }, }); export default organizationSlice.reducer;
Perkhidmatan Organisasi.jsx
import axios from "axios"; // Create Business const createOrganization = async (formData, token) => { const config = { headers: { Authorization: `Bearer ${token}`, }, }; const response = await axios.post( `${LOCAL}/api/v1/organization/create-business`, formData, config ); return response.data; }; const organizationService = { createOrganization, }; export default organizationService;
P粉5660487902024-04-07 11:17:19
Ia mestilah async({formData}, token), bukan async(formData, token)
Kemas kini: Nampaknya melakukan ini mengembalikan null dalam semua bidang
import axios from "axios"; // Create Business const createOrganization = async ({formData}, token) => { const config = { headers: { Authorization: `Bearer ${token}`, }, }; const response = await axios.post( `${LOCAL}/api/v1/organization/create-business`, formData, config ); return response.data; }; const organizationService = { createOrganization, }; export default organizationService;