0

I have to do bunch of ObjectKey and map just to render a list, why so much hassle, does firebase has built-in util for that?

onListenForMessages = () => {
    this.setState({ loading: true });

    this.props.firebase
      .messages()
      .orderByChild('createdAt')
      .limitToLast(this.state.limit)
      .on('value', snapshot => {
        const messageObject = snapshot.val();

        if (messageObject) {
          const messageList = Object.keys(messageObject).map(key => ({
            ...messageObject[key],
            uid: key,
          }));

          console.log('gg', messageList);

          this.setState({
            messages: messageList,
            loading: false,
          });
        } else {
          this.setState({ messages: null, loading: false });
        }
      });
  };
Hoknimo
  • 533
  • 2
  • 6
  • 15

1 Answers1

0

Update:You might wanna take a look at https://stackoverflow.com/a/51794212/324977 for support on native array operations on firestore

That's because array is basically not really supported in Firebase.

Firebase's Array Support Firebase has no native support for arrays. If you store an array, it really gets stored as an "object" with integers as the key names.

// we send this
['hello', 'world']
// Firebase stores this
{0: 'hello', 1: 'world'}

Reference: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

Alvin Theodora
  • 936
  • 1
  • 11
  • 18