【问题标题】:firebase throwing this typeError: getfirebase is not a functionfirebase throwing this typeError: getfirebase is not a function
【发布时间】:2019-10-17 20:50:17
【问题描述】:

绕过 firebase 身份验证,但它不断抛出以下 TypeError 消息并破坏应用程序:

TypeError: getfirebase 不是函数

如果我做错了,请查看我的商店和 authReducer。 在我的商店中,我为 reduxfirestore 提供了 getfirestore 和 getfirebase 到 react-redux-firebase。我正在使用 react-redux-firebase v2。

** store.js

import rrfConfig from "../config/rrfConfig";
import { reduxFirestore, getFirestore } from "redux-firestore";
import { reactReduxFirebase, getFirebase } from "react-redux-firebase";
import { createStore, compose, applyMiddleware } from "redux";
import rootReducer from "./reducers/rootReducer";
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/auth";
import firebaseConfig from "../config/fbConfig";
import thunk from "redux-thunk";
firebase.initializeApp(firebaseConfig);
firebase.firestore();

const initialState = {};

const store = createStore(
  rootReducer,
  initialState,
  compose(
    applyMiddleware(thunk.withExtraArgument({ getFirebase, getFirestore })),
    reactReduxFirebase(firebase, rrfConfig),
    reduxFirestore(firebase)
  )
);

export default store;

**authActions.js

export const signInAction = credentials => {
  return (dispatch, getState, { getfirebase }) => {
    const firebase = getfirebase();
    firebase
      .auth()
      .signInWithEmailAndPassword(credentials.email, credentials.password)
      .then(() => {
        dispatch({ type: "LOGIN_SUCCESS" });
      })
      .catch(err => {
        dispatch({ type: "LOGIN_ERROR", err });
      });
  };
};

**authReducer

const initialState = {
  authError: null
};

const authReducer = (state = initialState, action) => {
  switch (action.type) {
    case "LOGIN_ERROR":
      console.log("login error");
      return {
        ...state,
        authError: "login failed"
      };
    case "LOGIN_SUCCESS":
      console.log("login success");
      return {
        ...state,
        authError: null
      };
    default:
      return state;
  }
};

export default authReducer;

**rootReducer

import { combineReducers } from "redux";
import authReducer from "./authReducer";
import projectReducer from "./projectReducer";
import { firestoreReducer } from "redux-firestore";
import { firebaseReducer } from "react-redux-firebase";

const rootReducer = combineReducers({
  auth: authReducer,
  project: projectReducer,
  firestore: firestoreReducer,
  firebase: firebaseReducer
});

export default rootReducer;

package.json

{
  "name": "ghandhi-land",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "firebase": "^6.0.4",
    "materialize-css": "^1.0.0-rc.2",
    "node-sass": "^4.11.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^5.1.1",
    "react-redux-firebase": "^2.2.4",
    "react-router-dom": "^5.0.0",
    "react-scripts": "3.0.1",
    "redux": "^4.0.1",
    "redux-firestore": "^0.8.0",
    "redux-thunk": "^2.3.0"
  }
}

**登录组件

import { connect } from "react-redux";
import { signInAction } from "../../store/actions/authActions";
class SignIn extends Component {
  state = {
    email: "",
    password: " "
  };

  handleChange = e => {
    this.setState({
      [e.target.id]: e.target.value
    });
  };
  handleSubmit = e => {
    e.preventDefault();
    this.props.signIn(this.state);
  };

  render() {
    return (
      <div className="container">
        <form onSubmit={this.handleSubmit} className="white">
          <h5 className="grey-text text-darken-3">Sign In</h5>
          <div className="input-field">
            <label htmlFor="email">Email</label>
            <input type="email" id="email" onChange={this.handleChange} />
          </div>
          <div className="input-field">
            <label htmlFor="password">password</label>
            <input type="password" id="password" onChange={this.handleChange} />
          </div>
          <div className="input-field">
            <button className="btn pink lighten-1 z-depth-0">Login</button>
          </div>
        </form>
      </div>
    );
  }
}
const mapDispatchToProps = dispatch => {
  return {
    signIn: credentials => dispatch(signInAction(credentials))
  };
};

export default connect(
  null,
  mapDispatchToProps
)(SignIn);

【问题讨论】:

  • 您的代码看起来不错。你能console.loggetFirebasestore.js 中的值吗?另外,你到底在哪里得到TypeError 错误?是在执行signInAction方法的时候吗?
  • 当我在 console.log getfirebase 时,这就是它所说的 ƒ getFirebase() { if (!firebaseInstance) { throw new Error('Firebase instance does not yet exist. Check your compose function.'); } return firebaseInstance; }
  • 是的,它在执行 signInAction 时抛出了这个 typeError
  • @mgarcia。为什么 getfirebase 未定义?

标签: reactjs firebase redux


【解决方案1】:

当您编写 redux 中间件时,您将使用以下语句应用 thunk 中间件:

applyMiddleware(thunk.withExtraArgument({ getFirebase, getFirestore }))

作为额外参数传递的方法的名称采用驼峰表示法:getFirebasegetFirestore

但是,当您尝试检索 authActions.js 中的额外参数时,您将其解构为 getfirebase:

return (dispatch, getState, { getfirebase })

您应该使用相同的符号(驼峰式)解构您的对象。所以,你的代码应该是:

return (dispatch, getState, { getFirebase })

【讨论】:

  • 成功了。非常感谢您。我已经在这两天了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 2019-10-02
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多