Shuffle Data Records in Wix Code

In this free code sample you will learn to shuffle data records in Wix Code.
https://www.wixshow.com/wix-code-library/Shuffle-Data-Records-in-Wix-Code

2 Likes

This is great, but I am getting an error in use.

“Wix code SDK error: The data parameter that is passed to the data method cannot be set to value [object Promise]. It must be of type array.”

Here’s my usage…

// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixData from 'wix-data';

async function shuffleResult(resultingArray) {
 var ctr = resultingArray.length,
        temp, index;
 while (ctr > 0) {
        index = Math.floor(Math.random() * ctr);
        ctr--;
        temp = resultingArray[ctr];
        resultingArray[ctr] = resultingArray[index];
        resultingArray[index] = temp;
    }
 return resultingArray;
}

export async function getRandomTestimonials() {
 let max = 11 - 4; // Total number of testimonials - the number of testimonials we want returned; so we get atleast that many results
 let skip = Math.floor(Math.random() * max) + 1;
    wixData.query("Testimonials")
        .skip(skip)
        .limit(4)
        .find()
        .then(async (results) => {
           if (results.totalCount > 0) {
                  let items = results.items;
                  // console.log(items);
                  items = await shuffleResult(items);
                  return items;
           }
        })
        .catch((error) => {
              let errorMsg = error.message;
              let code = error.code;
        });
}

$w.onReady(function () {
 //TODO: write your page related code here...
 let testimonials = getRandomTestimonials();
    $w("#repeater2").data = testimonials;

    $w("#repeater2").onItemReady(($w, item, index) => {
        $w("#text27").text = item.name;
        $w("#text26").text = item.trip;
        $w("#text25").text = item.quote;
        $w("#image3").src = item.image;
    });
});

Update… I figured out what I was missing… See updated code below…

// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixData from 'wix-data';

async function shuffleResult(resultingArray) {
 var ctr = resultingArray.length,
        temp, index;
 while (ctr > 0) {
        index = Math.floor(Math.random() * ctr);
        ctr--;
        temp = resultingArray[ctr];
        resultingArray[ctr] = resultingArray[index];
        resultingArray[index] = temp;
    }
 return resultingArray;
}

export async function getRandomTestimonials() {
 let max = 11 - 4; // Total number of testimonials - the number of testimonials we want returned; so we get atleast that many results
 let skip = Math.floor(Math.random() * max) + 1;
 //////// FIXED: Left our "return" here
 return wixData.query("Testimonials")
        .skip(skip)
        .limit(4)
        .find()
        .then(async (results) => {
 if (results.totalCount > 0) {
 let items = results.items;
 // console.log(items);
                items = await shuffleResult(items);
 return items;
            }
        })
        .catch((error) => {
 let errorMsg = error.message;
 let code = error.code;
        });
}

//////// FIXED: Left our "async" here
$w.onReady(async function () {
 let testimonials = await getRandomTestimonials();
    $w("#repeater2").data = testimonials;

    $w("#repeater2").onItemReady(($w, item, index) => {
        $w("#text27").text = item.name;
        $w("#text26").text = item.trip;
        $w("#text25").text = item.quote;
        $w("#image3").src = item.image;
    });
});