【发布时间】: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.loggetFirebase在store.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 未定义?