0

I need to retrieve mutliple random data from my DB.

I have made a service in Sails.js for that purpose.

It's a Recursive function

  1. It generate a random number 0 to count of my DB.
  2. Stores the random number generated (All questions should be different)
  3. Get a random Question
  4. Store it in an array calling getQuestion And returning a promise
  5. If the random generated array is smaller than the nb Result wanted : We call the function again

     var RandomizerService= {        
          // get a random single question
          // return a promise
          getQuestion: function (limit, skip){
            var dfd = q.defer();
            Question.find().limit(limit).skip(skip).then(function (question){
              dfd.resolve(question);
             });
    
            return dfd.promise;
          },
    
          // Recursive function
          // Generate a random number 0 to count of my DB.
          // Stores the random number generated (All questions should be different)
          // Get a random Question and store it in an array calling getQuestion and returning a promise
          // If the random generated array is smaller than the nb Result wanted : 
          // We call the function again
    
          questions: function (count, tabRes, tabRand, nbRes){
            var dfd = q.defer(); 
            rand = Math.floor(Math.random() * (count - 1)) + 1;
    
            if ( tabRand.indexOf(rand) === -1) {
              tabRand.push(rand);
              RandomizerService.getQuestion(-1, rand).then(function (result){
    
                console.log(result[0]); // Is Returning AFTER the Randomizer.questions called in the Controller
                tabRes.push(result[0]);
                });
              }
              if (tabRand.length<nbRes){
                RandomizerService.questions(count, tabRes, tabRand, nbRes);  
              }
            dfd.resolve(tabRes);
            return dfd.promise;
          }
        };
        module.exports  = RandomizerService;
    

When I'm calling this function in my Controller

RandomizerService.questions(count, [], [], 30).then(function (randQuestions){ 
   console.log(randQuestions);  // Return [];
});

randQuestions in returning an empty array.

But the console.log(result[0]); inside the getQuestions promise is returning AFTER the RandomizerService.questions (in Controller) promise ! oO

(When mocking the find() with a standard promise the whole system works).

Is there a probleme with Waternline or Sails ? What am I doing wrong ?

I'm using Sails.js 0.11 and I use sails.disk but I will use mongDB in the future :)

Thanks guys :)

Luna
  • 496
  • 7
  • 18

1 Answers1

1

The first issue is that you're immediately resolving the promise, the second issue is that I think your if statements aren't setup correctly. Try this:

  questions: function (count, tabRes, tabRand, nbRes){
    var dfd = q.defer(); 
    rand = Math.floor(Math.random() * (count - 1)) + 1;

    if ( tabRand.indexOf(rand) === -1) {
      tabRand.push(rand);
      RandomizerService.getQuestion(-1, rand).then(function (result){

        console.log(result[0]); // Is Returning AFTER the Randomizer.questions called in the Controller
        tabRes.push(result[0]);

        if (tabRand.length<nbRes){
          RandomizerService.questions(count, tabRes, tabRand, nbRes)
          .then(function(res){
            dfd.resolve(res);
          })  
        } 
        else {
          dfd.resolve(tabRes);
        }
      });
    } 
    else if (tabRand.length<nbRes){
      RandomizerService.questions(count, tabRes, tabRand, nbRes)
      .then(function(res){
        dfd.resolve(res);
      }) 
    } 
    else {
      dfd.resolve(tabRes);
    }

    return dfd.promise;
  }
Yuri Zarubin
  • 11,439
  • 4
  • 30
  • 33
  • Thank you very much ! You were right, I was resolving the promise too ealy !! In fact I forget the last check of tabRand and recalling my function I still don't get why it worked with a mocked function oO Is it because of the find() that checked in my DB ? – Luna Feb 17 '15 at 19:24