我将布尔参数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 }); }
您正在将布尔参数
usingAsSignUp
传递给authenticateWithFirebase
突变端点的queryFn
,但它始终会导致undefined。这可能是因为您没有从组件正确传递参数。
要修复此错误,您需要将
usingAsSignUp
值作为signup
传递到authenticateWithFirebase
端点的queryFn
在您的firebaseApi
配置中。