1

I want to randomly pull a documents from a list of document. And currently, it works, but I will receive the same document again, but I don't want to.

let qnumber = Math.floor((Math.random() * 3) + 1);
const dialogflowAgentDoc = db.collection('esequiz').doc(''+qnumber);

So how do I edit it such that I do not pull any duplicates from the random document pulled?

So my cloud firestore looks like this, hence I use qnumber to determine a random number made up, and then called into db collection.

enter image description here]

Waelmas
  • 1,894
  • 1
  • 9
  • 19
Baboy
  • 111
  • 1
  • 1
  • 10

1 Answers1

0

You need to track IDs of retrieved documents and discard random IDs that were already retrieved.

Pseudo code:

class UniqueRandomIdProvider()
{
   alreadyRetrieved: number[] = [];

   public getNewRandomId(): number {
     while(true) {
     {
        const randomId = this.getRandomId();
        if (!this.alreadyRetrieved.contains(randomId) {
            return randomId;
        }   
     }

   }

   private getRandomId(): number {
      return Math.floor((Math.random() * 3) + 1);
   }
}
Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52
  • Sorry but i dont understand how to convert this into javascript language, since javascript is used in fulfillment dialogflow – Baboy Jan 15 '20 at 07:42
  • the document id is literally the numbers in the picture shown, left column – Baboy Jan 15 '20 at 07:45