How to recover firebase keys? Javascript

Asked

Viewed 132 times

0

Good evening, I have the following data structure: Firebase

I need to store all these Ids that are children of visits in an array to later create a loop to search for the data that each of these Ids has, but in the documentation there is no method to do this, so I found this site https://www.it-swarm.dev/pt/javascript/como-recuperar-varias-chaves-no-firebase/825394279/, there shows a very similar question, my question if there really is no other way to do it?

Note: My goal is to calculate how many tires were collected and how many were delivered per seller, so I need to access each one’s Ids, Nó aberto

Remembering that I am using javascript to later create a screen to consume this data follows my code:

 firebase.database().ref('visitas/').on('value', function (snapshot) {
        snapshot.forEach(function (item) {
            console.log(document.createTextNode(item.val()));
            resultado[i] = document.createTextNode(item.val());
            i++;
        })
    });
    console.log(resultado)

1 answer

2


Sometimes it is not even necessary to consult the documentation, you have already tried to make a console.log(item) just to know what’s inside? Inside you must find the key item.payload.doc.id which is the id of the document in question.

Anyway if you are thinking of doing something of the kind only to be able to count the amount of documents you have within a collection I suggest you stop here unless you have a lot of money to spend. Firebase is free, to a certain extent. Imagine the following scenario:

  • Within the collection visit you have 1000 documents.
  • Each time the user opens your app you read these 1000 documents only to get their id and then process the data you want.

Suppose in 1 day a single user visits this page 10 times, you will already get 10000 single user readings in a single collection, will not be enough to be charged, but the situation may get severe if the number of people using your app increases, so it is necessary to understand the functioning of Firebase and reduce to the maximum the number of operations done in the database per user. A solution to this problem would be to create a cloud function that control each time a document is written / deleted from collection A visits and based on the type of operation performed you update a single document in a collection B with the number of documents present and the values you need. This way instead of reading the 1000 documents A you will read a single document on B and which already contains the number of updated documents. From a look at this video to understand how Firebase charges its users.

  • I used 'item.key' so returned me the key, yes I was inattentive on the payment issue, thanks for the alert and I will give a studied on 'cloud Function', i opted for firebase due to the possibility of data synchronization even with the lack of features like internet and battery, thanks even!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.