How to get a random doc from a collection in firebase (JavaScript)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to get a random doc from a collection in firebase (JavaScript)?

For this task I’ll need to first receive the length of the collection, generate a random number with the collection length, and then grab a random document from the collection. How can this be achieved using JavaScript?

24th Jul 2022, 1:45 AM
Coder
Coder - avatar
1 Answer
0
firebase storage has a listAll() Documentation: https://firebase.google.com/docs/storage/web/list-files Reference: https://firebase.google.com/docs/reference/node/firebase.storage.Reference#listall Example: https://gist.github.com/sawaYch/f538d10d09ccf84cbff07e93a0b6cd0d You can then pick a random element of the items array in the ListResult. These items are item references, so you can directly call their .getMetadata() or .getDownloadURL() (depending on what you want to do)
24th Jul 2022, 7:39 AM
Alex
Alex - avatar