代码之家  ›  专栏  ›  技术社区  ›  mrbinky3000

当包含where查询参数时,firestoreconnect或firestore返回“未定义”

  •  0
  • mrbinky3000  · 技术社区  · 7 年前

    firebaseconnect hoc为“collections”返回未定义的值。但是,如果删除“where”参数,它将返回数据。

    function mapStateToProps(state) {
      return {
        contestants: state.firestore.ordered['ContestantsList-contestants'],
        votes: state.firestore.ordered['ContestantsList-votes'],
        currentUserId: state.firebase.auth.uid,
      }
    }
    
    export default compose(
      firestoreConnect(ownProps => {
        // added this console log to verify that ownProps.contestId exists.
        console.log('ownProps', {...ownProps})
        return ([
          {
            collection: 'votes',
            where: [
              ['authorId', '==', ownProps.currentUserId],
              ['contestId', '==', ownProps.contestId],
            ],
            storeAs: 'ContestantsList-votes'
          },
          {
            collection: 'contestants',
            orderBy: ['createdAt', 'asc'],
            where: ['contestId', '==', ownProps.contestId],  // <---- If I remove this line, it works
            storeAs: 'ContestantsList-contestants',
          },
        ])
      }),
      connect(mapStateToProps),
    )(ContestantsList);
    

    在上面的代码中, votes prop是一个数据数组,正如预期的那样。这个 contestants 道具返回为未定义。我完全不知道为什么会这样。我希望这对一个有经验的firestore用户来说是显而易见的。我是刚到firebase和firestore的。

    1 回复  |  直到 7 年前
        1
  •  0
  •   mrbinky3000    7 年前

    我确实解决了这个问题。这是一种种族状况。我创建了一个包含此查询的hoc,并用hoc包装了几个组件。其目的是使组件尽可能模块化,避免将数据通过组件传递给其他组件。

    我的方法意味着查询在呈现组件时运行了几次。它连续几次快速更新redux状态。随后出现了种族状况。

    我不得不重新组织我的项目,以便只进行一次查询,然后更新hoc以直接从redux读取结果。所以,最后是我的问题。