首页 > 解决方案 > 类型错误:firestore.collection 不是函数(React、Redux、Firestore)

问题描述

将 firebase 和 firestore 与 react 集成时,我遇到了这个错误。

我已经关注了新的 react-redux-firebase 文档,但它没有帮助。

错误-> TypeError:firestore.collection 不是函数

这是我收到错误的功能

代码

export const addProductFamily = (productFamilyDetails) => {
  return (dispatch, getState, { getFirestore }) => {
    console.log("product family details ", productFamilyDetails);
    const firestore = getFirestore();
    console.log("firestore is ", firestore);
    firestore
      .collection("productFamilyDetails") <------------ error
      .add({
        ...productFamilyDetails,
      })
  };
};

这是我设置所有配置的 App.js 文件

应用程序.js

import { Provider } from "react-redux";
import { createStore, applyMiddleware, combineReducers, compose } from "redux";
import thunk from "redux-thunk";
import authReducer from "./store/reducers/auth";
import {
  ReactReduxFirebaseProvider,
  firebaseReducer,
  getFirebase,
} from "react-redux-firebase";
import {
  createFirestoreInstance,
  firestoreReducer,
  getFirestore,
  reduxFirestore,
} from "redux-firestore";
import { firebaseConfig } from "./config/fbConfig";
import firebase from "firebase/app";

const rrfConfig = { userProfile: "users", useFirestoreForProfile: true };

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

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

const rrfProps = {
  firebase,
  config: rrfConfig,
  dispatch: store.dispatch,
  createFirestoreInstance,
};

ReactDOM.render(
  <Provider store={store}>
    <ReactReduxFirebaseProvider {...rrfProps}>
      <BrowserRouter>
        {/* <App /> */}
        <Layout />
      </BrowserRouter>
    </ReactReduxFirebaseProvider>
  </Provider>,
  document.getElementById("root")
);

我已经删除了不必要的导入

标签: reactjsfirebasegoogle-cloud-firestorereact-reduxreact-redux-firebase

解决方案


您调用 reduxFirestore 时的参数是向后的。改变这个:

reduxFirestore(firebaseConfig, firebase)

对此:

reduxFirestore(firebase, firebaseConfig)

推荐阅读