Datasets and Arrays

What I am trying to do is pull from a dataset only the entries that are approved (boolean value). I dump the dataset into an array and then loop through it on #repeater.onItemready. I push to another array only those that are approved. I want to then attach the “approved” array to the repeater so that is all that is displayed. My code is below. Where I log the new array with the data that I want, it logs perfectly. When I assign the various items in the array to the various fields in the repeater, it displays properly. The main issue that I have left is that when I try to assign the repeater.data to the array I keep getting an error that my new array is not an array? Seems weird as it was built by doing a push from another array and that I can access all the points like an array.

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

MY CODE:
//FILTER OUT UNAPPROVED
 export function unApproved() {

    wixData.query("Dispensories")
        .find()
        .then((results) => {
 
    $w("#repeater1").onItemReady(($item, itemData, index) => {
 let filterArray = results.items
 //console.log("index :" + index)
 //for (var i=0;i<filterArray.length;i++) {
 
 if (filterArray[index].approved === true) {
 var dispArray = []
 var myIndex = filterArray.indexOf(filterArray[index])
 //  console.log("MI "+myIndex)
 //console.log(filterArray[myIndex])
                    dispArray.push(filterArray[myIndex])
                    console.log(dispArray[0].title)
                    console.log(dispArray[0].city)
                    console.log(dispArray[0].phone) 
                    $w("#repeater1").data = dispArray[0];
            }
 //console.log(dispArray.length)
 //
            $item("#nameTxt").text = dispArray[0].title;
            $item("#cityTxt").text = dispArray[0].city;
            $item("#phoneTxt").text = dispArray[0].phone;
            $item("#image1").src = dispArray[0].photo;
 //$item("viewMoreBtn").link  = DONE VIA THE CONNECTOR */
        });
    });
 
} // END OF FILTER UNAPPROVED///////////////////

Any help here would be great… I feel like I am either really close or WAY off! :wink:

You need this line at the top of your function or in your onReady:

$w('#repeater1').data = [];
The rest of your code should be fine.

I put that there and am still getting the same error.

@info84653 Ok, I probably shouldn’t have said your code was fine since I only skimmed through it.

On closer look, there’s no need for dispArray to exist at all if you are only going to display the first item. I’m not sure if that’s your intention, but I’ll assume so since that’s what your code is doing, so in that case you can use limit(1) parameter instead. Also, there’s a wixData query parameter to make your code shorter, more readable, and more efficient.

The reason you’re getting that error is because you’re assigning the repeater array to only the first item in dispArray, which is an object and not an array. Repeaters only display arrays.

export function unApproved() {
    wixData.query("Dispensories")
        .eq("approved", true)
        .limit(1)
        .find()
        .then((results) => {
             const filterArray = results.items;
    $w("#repeater1").onItemReady(($item, itemData, index) => {
            $item("#nameTxt").text = filterArray.title;
            $item("#cityTxt").text = filterArray.city;
            $item("#phoneTxt").text = filterArray.phone;
            $item("#image1").src = filterArray.photo;
        });
    });
}

@David - SKmedia Just showing one is not the intention. I want to show all entries that are marked as approved in the dataset. So the goal is to look at Dispensories and then display all that are approved so the repater should show all of that. Make sense?

I have this now and it works for the most part except it does not list the last one in the list.

export function unApproved() {
    wixData.query("Dispensories")
        .eq("approved", true)
 //.limit(1)
        .find()
        .then((results) => {
 const filterArray = results.items;
 
    $w("#repeater1").onItemReady(($item, itemData, index) => {
 //console.log(filterArray.length)
 //for (var i=0;i<filterArray.length;i++) {
                console.log("INDEX "+index)
                $w("#repeater1").data = filterArray;
                $item("#nameTxt").text = filterArray[index].title;
                $item("#cityTxt").text = filterArray[index].city;
                $item("#phoneTxt").text = filterArray[index].phone;
                $item("#image1").src = filterArray[index].photo;
 //}
        });
    });
}

If only one item on the list isn’t showing, try replacing [index] with [index - 1]

@skmedia That makes it so that the first one is skipped. Feel like the Index of the repeater onReady and the index of the array are not lining up.

INDEX 0
LENGTH 4
INDEX 1
LENGTH 4
INDEX 2
LENGTH 4
INDEX 3
LENGTH 4
INDEX 4
LENGTH 4

Index is the index from the onReady and the Length is the filterArray. The array is going to top out at filterAray[3] and the index is going to go one further hence the issue we are seeing.

@info84653 So that’s the console output? Weird…

Maybe it’s a timing issue? Try this:

export async function unApproved() {
wixData.query(“Dispensories”)
.eq(“approved”, true )
.find()
.then((results) => {
await filterArray = results.items;

$w("#repeater1").onItemReady(($item, itemData, index) => { 

//console.log(filterArray.length)
console.log(“INDEX “+index)
$w(”#repeater1”).data = filterArray;
$item(“#nameTxt”).text = filterArray[index].title;
$item(“#cityTxt”).text = filterArray[index].city;
$item(“#phoneTxt”).text = filterArray[index].phone;
$item(“#image1”).src = filterArray[index].photo;
});
});
}

Ok… will tinker with this but it is currently giving me the following

Ok… for whatever reason, what seems to be happening is that whenever the onReady looping finds the first uncheck or unapproved entry, it skips the next true for some reason

Yes, sorry the async was in the wrong scope, it should have been async (results) =>

Post your loop. Maybe you’ve used continue where it shouldn’t be or something.

@skmedia Here is the entire function…

export async function unApproved() {
wixData.query(“Dispensories”)

    .eq("approved", true) 
    .find() 
    .then((results)=> { 
    await filterArray = results.items; 


$w("#repeater1").onItemReady(($item, itemData, index) => { 

//console.log(filterArray.length)
console.log(“INDEX “+index)
$w(”#repeater1”).data = filterArray;
$item(“#nameTxt”).text = filterArray[index].title;
$item(“#cityTxt”).text = filterArray[index].city;
$item(“#phoneTxt”).text = filterArray[index].phone;
$item(“#image1”).src = filterArray[index].photo;
});
});
}

@info84653 Didn’t you say you had a loop in your onReady though?

@skmedia I just got this sorted about 30 mins ago by something I saw in the ADI updates email that i got this AM! Thanks much for all your help. I am on to the next issue now! :wink:

1 Like