I'm very much a beginner at Retool and coding in general, and can't find a simple way to get the document_id included in my Firestore query results.
I've an App built with screens using data from queries of Firestore. But the user collection in Firestore does not have a userId field, rather the unique user id is the document_id. I'd therefore like the document_id available in my Retool app query results. Would Event Handlers / Success be the place for script to add the document_ids?
To include the document_id in your Firestore query results, let's try to the following:
Modify your Firestore Query: Within your Firestore query, you can add a projection field to explicitly include the document_id. This projection field should be named "name". For instance, if your current query retrieves documents from a collection named "users", you would modify it as follows:
db.collection('users').select('__name__').get().then((querySnapshot) => {
// Process query results here
});
Access the document_id in Retool: Once you've modified your query to include the document_id, you can access it directly within your Retool app. The document_id will be available as a property of each document object in the query results. For example, if you're using a Table component to display the query results, you can access the document_id using the following expression:
{{ data.documentId }}
This expression will display the document_id for the corresponding row in the table.
Regarding the use of Event Handlers / Success, you can certainly use them to add custom logic to your application, but they are not specifically required for retrieving the document_id. As described above, you can directly access the document_id property of the document objects in your query results.
Hi Patrick, delighted to have some help! I'm still struggling though, as I can't see how to modify my initial query which is done inside the Retool App build, and I can't find how to add a projection field? Also, I'm after pulling back all user documents, is this what adding the projection "name" field will do or will it act like a where condition? Thx again Nico.