Querying user specific data (incl. population) with react-redux-firebase in one compose method

☆樱花仙子☆ 提交于 2019-12-24 14:28:37

问题


i'm working with react-native and have the problem to load only user specific data with firebaseConnect. Imagine there are thousends of todos, users and doers and don't want to reference all elements of them.

First of all, a shortcut of my data structure in the real time database:

- todos
  |- cleanRoom
  |    |- createdBy: alica
  |    |- title: "Clean the room up"
  |    |- doerList
  |        |- roomCleanerMichael
  |        |- roomCleanerAlica
  |- playBall
       |- createdBy: michael
       |- title: "Play the ball"
       |- doerList
           |- ballPlayerAlica

- users
  |- michael
  |    |- name: Michael
  |    |- age: 22
  |- alica
       |- name: Alica
       |- age: 27

- doers
  |- roomCleanerMichael
  |    |- user: michael
  |    |- status: done
  |- roomCleanerAlica
  |    |- user: alica
  |    |- status: done
  |- ballPlayerAlica
       |- user: alica
       |- status: done

I have a view/screen in RN, where all informations should be displayed. For now, i have...

const populatesTodoCreator = [{child: 'createdBy', root: 'users'}]

const mapStateToProps = (state, props) => {
  return {
    todos: populate(state.firebase, 'todos', populatesTodoCreator),
    uid: state.firebase.auth.uid
  }
}

export default compose(
  firebaseConnect((props, store) => {
      return [
        {path: 'todos', populatesTodoCreator},
      ]
    }
  ),
  connect(mapStateToProps, mapDispatchToProps)
)(TodoOverviewScreen)

When i log in the application with firebase.auth.uid = michael, i only want to reference the list of todos, where michael participates in as doer and populate all doers in doersList (including the user in a specific doer), and that all in only one single compose method.

So the resulting redux state should look like this, because michael is only a doer for cleanRoom:

- todos
  |- cleanRoom
      |- createdBy
      |    |- name: Alica
      |    |- age: 27
      |- title: "Clean the room up"
      |- doerList
           |- roomCleanerMichael
           |     |- user
           |     |    |- name: Michael
           |     |    |- age: 22
           |     |- status: done
           |- roomCleanerAlica
                 |- user
                 |    |- name: Alica
                 |    |- age: 27
                 |- status: done

Is this task possible? If yes, how do i get this done? Any structural suggestions?


回答1:


Populating a list like doerList is done by another populate parameter.

Changing the populate array to...

const populatesTodoCreator = [
  { child: 'createdBy', root: 'users' },
  { child: 'doerList', root: 'doers' } // populates the whole list of elements
]

worked for me.

Querying only the todos, where the logged in user is participating in and populating the user within the doer object (to get the expected result) is another question.



来源:https://stackoverflow.com/questions/48198537/querying-user-specific-data-incl-population-with-react-redux-firebase-in-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!