0

The layout of my data in Firestore:

User documents contain an array of id references to chat documents.

Ex:

collections
    /users
        'CQATa3f3d2XmBskmmt8hmCJhzcJ2'
             name: '...'
             email: '...'
             chats: ["RuUKEwsGtR9QylicdgJW", "JlzcIfkZ1KzeXClhJvOE"]
    /chats
        ...    

For the current user, I want to get the chat object associated with each element in its chat array. This data should go to firestore.ordered so I can supply it to a FlatList, but firestore.ordered.chats is always undefined, even though firestore.data.chats has the correct data.

I'm not sure if this is an issue having to do with the order in which I call connect and firestoreConnect or how I am using populates, or something else.

... Component ...


const populates = [
  { child: 'chats', root: 'chats' }
]

const mapStateToProps = (state, props) => { 
  return {
    auth: state.firebase.auth,
    user: populate(state.firestore, 'users', populates),
    chats: state.firestore.ordered.chats,
  }
}

// ---- TEST ----
export default compose(
  connect(mapStateToProps, mapDispatchToProps),
  firestoreConnect((props) => {
    return [ { collection: 'users', doc: props.auth.uid, populates } ]
  }),
)(Main)

Result of logging state inside of mapStateToProps:

enter image description here

Greg M
  • 65
  • 2
  • 9

1 Answers1

0

I ended up just getting the array of chats from the result of the populate call.

const populates = [
{ child: 'chats', root: 'chats', keyProp: 'subjectName', queryParams: ['orderByKey'] } 
]


const mapStateToProps = (state, props) => { 
  const populatedUser = populate(state.firestore, 'users', populates)
  let chats = null
  if(populatedUser){
    chats = Object.values(populatedUser[state.firebase.auth.uid].chats)
  }
  return {
    auth: state.firebase.auth,
    user: populatedUser,
    chats: chats
  }
}

// ---- TEST ----
export default compose(
  connect(mapStateToProps, mapDispatchToProps),
  firestoreConnect((props) => {
    return [ { collection: 'users', doc: props.auth.uid, populates } ]
  }),
)(Main)
Greg M
  • 65
  • 2
  • 9