无法从Firestore中检索数据“ TypeError:this.store未定义”

时间:2018-12-17 18:48:32

标签: reactjs redux react-redux react-redux-firebase

我一直在尝试从Firestore检索我的数据收集。但这是问题

error goes like this

  import React, { Component } from 'react'
  import Notification from './Notification.js'
  import ProjectList from '../projects/ProjectList'
  import { connect } from 'react-redux'
  import { compose } from 'redux'
  import { firestoreConnect } from 'react-redux-firebase'

  class Dashboard extends Component {
    render() {
      //console.log(this.props)
      const { projects } = this.props
      return(
        <div className="dashboard container">
          <div className="row">
            <div className="col s12 m6">
              <ProjectList projects={projects}  />
            </div>
            <div className="col s12 m5 offset-m1">
              <Notification />
            </div>
          </div>
        </div>
      )
    }
  }

  export default compose(
    firestoreConnect(['projects']),
    connect((state) => ({
      projects: state.firestore.ordered.projects
    }))
  )(Dashboard)

这是我的减根剂

  import authReducer from './authReducer'
  import projectReducer from './projectReducer'
  import { combineReducers } from 'redux'
  import { firestoreReducer } from 'redux-firestore'

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

  export default rootReducer

我的项目精简器就是这样

  const initState = {
    projects: [
      {id: '0', title: 'do some JavaScript', content: 'blah blah blah...'},
      {id: '1', title: 'grab some vegitables', content: 'blah blah blah...'},
      {id: '2', title: 'have a cup of coffee', content: 'blah blah blah...'}
    ]
  }

  const projectReducer = (state = initState, action) => {
    switch (action.type) {
      case 'CREATE_PROJECT':
            console.log('project is', action.project)
            return state
      case 'CREATE_PROJECT_ERR':
            console.log('create project error', action.err)
            return state
      default:
      return state
    }
  }

  export default projectReducer

动作

  export const createProject = (project) => {
    return (dispatch, getState , { getFirebase, getFirestore }) => {
      // console.log('project: ', project)

      // adding data to firestore
      const firestore = getFirestore()
      firestore.collection('projects').add({
        ...project,
        authorFirstName: 'Herold',
        authorSecondName: 'Finch',
        authorId: 111,
        createdAt: new Date()
      }).then(() => {
        dispatch({type:'CREATE_PROJECT', project })
      }).catch((err) => {
        dispatch({type:'CREATE_PROJECT_ERR', err })
      })
    }
  }

当我从projectReducer中获取静态数据时,一切都很好。但是我无法理解从数据库检索数据时缺少的地方。 我尝试降级react-redux-firebase,react-redux仍然无法正常工作。请帮助我解决此问题,或者我需要知道是否还有其他必须选择的方法。预先感谢!

2 个答案:

答案 0 :(得分:0)

CREATE_PROJECT的动作创建者在哪里?

答案 1 :(得分:0)

我遇到了类似的问题,我的是 TypeError: Cannot read property 'firestore' of undefined, 尝试使用函数作为 firestoreConnect 中的参数;像这样的东西 firestoreConnect(() => [{ collection: 'projects' }])

export default compose(
    connect(mapStateToProps),
    firestoreConnect(() => [
       { collection: 'projects' }
    ])
)(Dashboard)