我将布尔参数 usingAsSignUp
传递到 queryFn
中。
不幸的是,usingAsSignUp
总是导致未定义
!我如何获得它的值? usingAsSignUp
是由使用组件中的 useEffect
设置的状态。
RTK 查询 createApi
与 queryFn
:
export const firebaseApi = createApi({ reducerPath: "firebaseApi", baseQuery: fakeBaseQuery(), tagTypes: ["Auth"], //Optional, https://redux-toolkit.js.org/rtk-query/api/createApi#tagtypes endpoints: (builder) => ({ authenticateWithFirebase: builder.mutation({ async queryFn({ email, password, usingAsSignUp }) { try { const auth = getAuth(firebaseApp); const userCredential = usingAsSignUp ? await createUserWithEmailAndPassword(auth, email, password) : await signInWithEmailAndPassword(auth, email, password); return { data: { uid: userCredential?.user?.uid, email: userCredential?.user?.email, usingAsSignUp: usingAsSignUp, }, }; } catch (e) { return { error: e }; } }, providesTags: ["Auth"], //Optional, https://redux-toolkit.js.org/rtk-query/api/createApi#providestags }), }), }); export const { useAuthenticateWithFirebaseMutation } = firebaseApi;
使用useEffect
使用组件来设置传递给queryFn
的状态:
import { useAuthenticateWithFirebaseMutation } from "../../persistence/apiSlices"; const [signup, setSignup] = useState(true); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const location = useLocation(); const [authenticateNow, result, data] = useAuthenticateWithFirebaseMutation(); useEffect(() => { location.pathname === "/login" ? setSignup(false) : setSignup(true); }, [location.pathname] ); async function onSubmitACB() { await authenticateNow({ email, password, signup }); }
P粉5292450502023-09-14 00:34:54
您正在将布尔参数 usingAsSignUp
传递给 authenticateWithFirebase
突变端点的 queryFn
,但它始终会导致 undefined< /代码>。这可能是因为您没有从组件正确传递参数。
要修复此错误,您需要将 usingAsSignUp
值作为 signup
传递到 authenticateWithFirebase
端点的 queryFn
在您的 firebaseApi
配置中。
await authenticateNow({ email, password, usingAsSignUp: signup });