【问题标题】:getFirebase is not a function error using firebase auth create usergetFirebase 不是使用 firebase auth create user 的函数错误
【发布时间】:2020-04-19 15:45:32
【问题描述】:

试图搜索如何解决我的问题,但仍然无法弄清楚。

我使用 Formik 进行注册,我想将值传递给我的 firebase。

我想得到这样的东西(查看图片) firebase auth credentials

我会关注的教程。 https://www.youtube.com/watch?v=J_SEMZKFxtw 6:06 时间范围。

authActions.js

export const signUp = data => async (
   dispatch, 
   getState, 
   {getFirebase}
   ) => {
      const firebase = getFirebase()
   try {
      const res = await firebase
      .auth()
      .createUserWithEmailAndPassword(data.email, data.password);
      console.log(res);
 } catch(err) {}
};

signUp.js

import React from 'react';
import { connect } from 'react-redux';
import { Formik, Field } from 'formik';
import * as Yup from 'yup';

import { FormWrapper, StyledForm } from '../../../hoc/layout/elements';
import Input from '../../../components/UI/Forms/Input/Input';
import Button from '../../../components/UI/Forms/Button/Button';
import Heading from '../../../components/UI/Headings/Heading';

import * as actions from '../../../store/actions';

const SignUpSchema = Yup.object().shape({
    firstName: Yup.string()
    .required('Your first name is required')
    .min(3, 'Too short.')
    .max(25, 'Too long.'),
    lastName: Yup.string()
    .required('Your last name is required')
    .min(3, 'Too short.')
    .max(25, 'Too long.'),
    email: Yup.string()
    .email('Invalid email.')
    .required('The email is required.'),
    password: Yup.string().required('The password is required.').min(8, 'The password is too short'),
    confirmPassword: Yup.string()
    .oneOf([Yup.ref('password'), null], `Password doesn't match`)
    .required('You need to confirm your password.'),
})

const SignUp = ({ signUp }) => {
    return (
           <Formik 
            initialValues={{
                firstName: '',
                lastName: '',
                email: '',
                password: '',
                confirmPassword: ''
           }}
           validationSchema={SignUpSchema}
           onSubmit={(values, { setSubmitting }) => {
               console.log(values);
               signUp(values)
               setSubmitting(false)
           }}
           >
            {({isSubmitting, isValid}) => (
                <FormWrapper>
                    <Heading size="h1" color="white" noMargin>Sign up for an account</Heading>
                    <Heading size="h4" color="white">Fill in your details to register your new account</Heading>
                    <StyledForm>
                        <Field type='text' name='firstName' placeholder="Your first name" component={Input} />
                        <Field type='text' name='lastName' placeholder="Your last name" component={Input} />
                        <Field type='email' name='email' placeholder="Your email..." component={Input} />
                        <Field
                        type='password'
                        name='password'
                        placeholder="Your password..."
                        component={Input}
                        />
                        <Field
                        type='password'
                        name='confirmPassword'
                        placeholder="Re-type your password..."
                        component={Input}
                        />
                        <Button disabled={!isValid} type="submit">Sign up</Button>
                    </StyledForm>
                </FormWrapper>
            )}
           </Formik>
    );
};

const mapStateToProps = (state) => ({

})

const mapDispatchToProps = {
    signUp: actions.signUp,
};

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(SignUp);

./actions/index.js

export { signUp } from './authActions';

【问题讨论】:

    标签: reactjs firebase firebase-authentication


    【解决方案1】:

    您需要使用 react-redux-firebase 包并使用中间件 thunk 对其进行配置,以便能够在 redux 操作中使用 getFirebase

     import thunk from 'redux-thunk';
     import { getFirebase } from 'react-redux-firebase';
    
     const store = createStore(
      rootReducer,
      initialState,
      compose(
        applyMiddleware([
          // Pass getFirebase function as extra argument
          thunk.withExtraArgument(getFirebase)
        ])
      )
    );
    

    查看documentation了解更多信息

    此外,在您的动作创建器中,您不需要从第三个参数中解构 getFirebase。它实际上是第三个参数

    export const signUp = data => async (
       dispatch, 
       getState, 
       getFirebase
       ) => {
          const firebase = getFirebase()
       try {
          const res = await firebase
          .auth()
          .createUserWithEmailAndPassword(data.email, data.password);
          console.log(res);
     } catch(err) {}
    };
    

    【讨论】:

    • 谢谢,我的商店里已经有了这个功能,但是 { getFirebase } 仍然无法从我的 authAction.js 文件中运行。这是我的createReduxStore.js 文件中的代码
    • 更新了我的答案,问题在于您如何在动作创建者中使用 getFirebase
    • 感谢您的回答并尝试过,但这就是我的控制台上显示的内容。 “POST googleapis.com/identitytoolkit/v3/relyingparty/…400”错误。
    猜你喜欢
    • 2020-03-31
    • 2021-09-12
    • 2016-01-23
    • 2016-09-11
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 2018-11-27
    • 2020-07-01
    相关资源
    最近更新 更多