Function getting firebase object returns hard to use object

What I am trying to do

I am creating a social media app with react native and firebase. I am trying to call a function, and have that function return a list of posts from off of my server.

Problem

Using the return method on a firebase query gives me a hard to use object array:

Array [

   Object {
   "-L2mDBZ6gqY6ANJD6rg1": Object {
      //...
     },
   },

]

I don't like how there is an object inside of an object, and the whole thing is very hard to work with. I created a list inside my app and named it items, and when pushing all of the values to that, I got a much easier to work with object:

Array [
Object {
 //...
   "key": "-L2mDBZ6gqY6ANJD6rg1",
  },
]

This object is also a lot nicer to use because the key is not the name of the object, but inside of it.

I would just return the array I made, but that returns as undefined.

My question

In a function, how can I return an array I created using a firebase query? (to get the objects of an array)

My Code

runQ(group){
var items = [];
//I am returning the entire firebase query...
return firebase.database().ref('posts/'+group).orderByKey().once ('value', (snap) => {
      snap.forEach ( (child) => {       
       items.push({
         //post contents
     });
     });
console.log(items)
//... but all I want to return is the items array. This returns undefined though.
})
}

1 answer

  • answered 2018-01-14 07:27 K.Wu

    Please let me know if I'm getting your question correctly. So, the posts table in database looks like this right now:

    enter image description here

    And you want to return these posts in this manner:

    [
        {
            "key": "-L1ELDwqJqm17iBI4UZu",
            "message": "post 1"
        },
        {
            "key": "-L1ELOuuf9hOdydnI3HU",
            "message": "post 2"
        },
        {
            "key": "-L1ELqFi7X9lm6ssOd5d",
            "message": "post 3"
        },
        {
            "key": "-L1EMH-Co64-RAQ1-AvU",
            "message": "post 4"
        }
        ...
    ]
    

    Is this correct? If so, here's what you're suppose to do:

    var items = [];
    firebase.database().ref('posts').orderByKey().once('value', (snapshot) => {
        snapshot.forEach((child) => {
            // 'key' might not be a part of the post, if you do want to
            // include the key as well, then use this code instead
            //
            // const post = child.val();
            // const key = child.key;
            // items.push({ ...post, key });
            //
            // Otherwise, the following line is enough
            items.push(child.val());
        });
    
        // Then, do something with the 'items' array here
    })
        .catch(() => { });
    

    Off the topics here: I see that you're using firebase.database().... to fetch posts from the database, are you using cloud functions or you're fetching those posts in your App, using users' devices to do so? If it's the latter, you probably would rather use cloud functions and pagination to fetch posts, mainly because of 2 reasons:

    1. There might be too many posts to fetch at one time

    2. This causes security issues, because you're allowing every device to connect to your database (you'd have to come up with real good security rules to keep your database safe)