Can't get object key in firebase

Viewed 889

Problem: I am making an app with Firebase and React Native where users can post, and then other users can comment on posts. To do the comments, I need to grab the key of the content of a post. I tried using the child.getKey() function, but that gave me an error.

child.getKey is not a function. (In 'child.getKey()', 'child.getKey' is undefined)

I would really love some help getting the key. Thank you!

Code

  getItems(){
    var items = [];
    var query = ref.orderByKey();
    query.once ('value', (snap) => {
      snap.forEach ( (child) => {       
       items.push({
         content: child.val().content,
         key: child.getKey()
     });
     });
    items.reverse();
    }).then(() => {
        this.setState({firebaseItems: items});
    });
 }

Firebase layout

Posts:
    -Kier498dma39md:
        content: 'This is an example post. The numbers above me are an example of the random key I am trying to get.'
1 Answers

So, I did some research, and eventually found out all I needed to do was use key: child.key

Related