Problem with WixData query

Hello,
I have a site I am working on which I am making certain elements on my page (mostly images) visible or hidden depending on what is in the database collection. I have a drop down box where different users can be selected. The wix query, then checks the database for certain values to be found and enables related images, leaving others hidden.

This works very well, sometimes, intermittently, in the middle of the wix query, I get two different XHR Failed Loading: POST errors.

I am not sure entirely if its because how I coded this, or if this is some wix error. But I find it odd that the same query, sometimes has a problem and sometimes does not. I’ve made it an ASYNC function and AWAIT the wixdata.query, Sometimes when the var i is 5 , or 7, or 21, or 23, it varies, but these two POST errors pop up, and my desired function fails because of it, it needs to get through the loop w/o these post errors. ).

or it just works. .

Is there a way to set up an error handler and redrive the loop or what?

export async function checkTheBoxes()
{
        let endEmp = AllEmperors.length 
    //  let userEmail = await wixUsers.currentUser.getEmail()
        let value2 = memory.getItem("userNick");
        let value3 = memory.getItem("userCEmail")
        let colNotSet = memory.getItem("colNotSet")
        
        for (var i=0;i<endEmp+1;i++) { 
            let results = await wixData.query('RomanImperialCoins').eq('user', useremail) 
                .eq('title', AllEmperors[i])
                .limit(1000)
                .ascending('title')
                .find()     
                .then((results) => 
                {
                     console.log('i=',i,'AllEmperors[i]=',AllEmperors[i])
                    if (results.length === 0)
                    {
                        console.log('its 0')
                        console.log('usereamil.length', useremail.length)
                    }
                    let numCoins = results.totalCount
                    if (numCoins > 0)
                    {
                        if (i === 1)
                        {
                            $w('#txtLoading').hide()
                            $w('#vespasianCheck').show()
                            $w('#VespasianX').hide()
                            $w('#Vespasian').link  = link
                            $w('#Vespasian').target = "_self";
                        }
                        else if (i === 2)
                        {
                            $w('#txtLoading').show()
                            $w('#LiciniusCheck').show()
                            $w('#LiciniusX').hide()
                            $w('#Licinius').link  = link
                            $w('#Licinius').target = "_self";
                        }   
                        .
                         .  omitted
                          .

Any help would be appreciated.

Thanks!

Where is the rest of the code?

Hello Bruno, Thank you for responding, I wasn’t sure how much to provide. The routine the error is showing up in, is a couple hundred lines. AllEmperors[] is defined out at the top of the code for the page, before the onReady().
Let me know if you need any more.

let AllEmperors = ["unused","VESPASIAN", "LICINIUS I", "AUGUSTUS", "TIBERIUS", 
                    "CALIGULA", "CLAUDIUS", "NERO", "GALBA", "OTHO", "VITELLIUS",
                   "TITUS", "DOMITIAN", "NERVA", "TRAJAN", "HADRIAN", 
                   "ANTONINUS PIUS", "MARCUS AURELIUS", "LUCIUS VERUS",                 
                   "COMMODUS","PERTINAX","DIDIUS JULIANUS", "SEPTIMIUS SEVERUS", 
                    "PESCENNIUS NIGER", "CLODIUS ALBINUS"]


export async function checkTheBoxes()
{
        let endEmp = AllEmperors.length 
    //  let userEmail = await wixUsers.currentUser.getEmail()
        let value2 = memory.getItem("userNick");
        let value3 = memory.getItem("userCEmail")
        let colNotSet = memory.getItem("colNotSet")
        
        for (var i=0;i<endEmp+1;i++) { 
            let results = await wixData.query('RomanImperialCoins').eq('user', useremail) 
                .eq('title', AllEmperors[i])
                .limit(1000)
                .ascending('title')
                .find()     
                .then((results) => 
                {
                     console.log('i=',i,'AllEmperors[i]=',AllEmperors[i])
                    if (results.length === 0)
                    {
                        console.log('its 0')
                        console.log('usereamil.lenmgth', useremail.length)
                    }
                    let numCoins = results.totalCount
                    if (numCoins > 0)
                    {
                        if (i === 1)
                        {
                            $w('#txtLoading').hide()
                            $w('#vespasianCheck').show()
                            $w('#VespasianX').hide()
                            $w('#Vespasian').link  = link
                            $w('#Vespasian').target = "_self";
                        }
                        else if (i === 2)
                        {
                            $w('#txtLoading').show()
                            $w('#LiciniusCheck').show()
                            $w('#LiciniusX').hide()
                            $w('#Licinius').link  = link
                            $w('#Licinius').target = "_self";

                        }   
                        else if (i === 3)
                        {
                        //  $w('#txtLoading').hide()
                            $w('#AugustusCheck').show()
                            $w('#AugustusX').hide()
                            $w('#Augustus').link  = link
                            $w('#Augustus').target = "_self";                           
                        }
                        else if (i === 4)
                        {
                            $w('#txtLoading').hide()
                            $w('#TiberiusCheck').show()
                            $w('#TiberiusX').hide()     
                            $w('#Tiberius').link  = link
                            $w('#Tiberius').target = "_self";
                        }
                        else if (i === 5)
                        {
                            $w('#txtLoading').show()
                            $w('#CaligulaCheck').show()
                            $w('#CaligulaX').hide()
                            $w('#Caligula').link  = link
                            $w('#Caligula').target = "_self";
                            
                                
                        }
                        else if (i === 6)
                        {
                        //  $w('#txtLoading').show()
                            $w('#ClaudiusCheck').show()
                            $w('#ClaudiusX').hide()
                            $w('#Claudius').link  = link
                            $w('#Claudius').target = "_self";
                            
                                $w('#txtLoading').expand()
                        }   
                        else if (i === 7)
                        {
                            $w('#txtLoading').hide()
                            $w('#NeroCheck').show()
                            $w('#NeroX').hide()
                            $w('#Nero').link  = link
                            $w('#Nero').target = "_self";
                        }           
                        else if (i === 8)
                        {
                            $w('#txtLoading').show()
                            $w('#GalbaCheck').show()
                            $w('#GalbaX').hide()
                            $w('#Galba').link  = link
                            $w('#Galba').target = "_self";
                        }   
                        else if (i === 9)
                        {
                    //      $w('#txtLoading').hide()
                            $w('#OthoCheck').show()
                            $w('#OthoX').hide()
                            $w('#Otho').link  = link
                            $w('#Otho').target = "_self";
                        }                                                                                   
                        else if (i === 10)
                        {
                            $w('#txtLoading').hide()
                            $w('#VitelliusCheck').show()
                            $w('#VitelliusX').hide()
                            $w('#Vitellius').link  = link
                            $w('#Vitellius').target = "_self";
                        }       
                        else if (i === 11)
                        {
                            $w('#txtLoading').show()
                            $w('#TitusCheck').show()
                            $w('#TitusX').hide()
                            $w('#Titus').link  = link
                            $w('#Titus').target = "_self";
                        }                   
                        else if (i === 12)
                        {
                            //$w('#txtLoading').show()
                            $w('#DomitianCheck').show()
                            $w('#DomitianX').hide()
                            $w('#Domitian').link  = link
                            $w('#Domitian').target = "_self";
                        }
                        else if (i === 13)
                        {
                            $w('#txtLoading').hide()
                            $w('#NervaCheck').show()
                            $w('#NervaX').hide()
                            $w('#Nerva').link  = link
                            $w('#Nerva').target = "_self";
                        }   
                        else if (i === 14)
                        {
                            $w('#txtLoading').show()
                            $w('#TrajanCheck').show()
                            $w('#TrajanX').hide()
                            $w('#Trajan').link  = link
                            $w('#Trajan').target = "_self";
                        }   
                        else if (i === 15)
                        {
                            //$w('#txtLoading').hide()
                            $w('#HadrianCheck').show()
                            $w('#HadrianX').hide()
                            $w('#Hadrian').link  = link
                            $w('#Hadrian').target = "_self";
                        }                           
                        else if (i === 16)
                        {
                            $w('#txtLoading').hide()
                            $w('#AntoninusPiusCheck').show()
                            $w('#AntoninusPiusX').hide()
                            $w('#AntoninusPius').link  = link
                            $w('#AntoninusPius').target = "_self";
                        }
                        else if (i === 17)
                        {
                            $w('#txtLoading').show()
                            $w('#MarcusAureliusCheck').show()
                            $w('#MarcusAureliusX').hide()
                            $w('#MarcusAurelius').link  = link
                            $w('#MarcusAurelius').target = "_self";
                        }
                        else if (i === 18)
                        {
                        //  $w('#txtLoading').show()
                            $w('#LuciusVerusCheck').show()
                            $w('#LuciusVerusX').hide()
                            $w('#LuciusVerus').link  = link
                            $w('#LuciusVerus').target = "_self";
                        }
                        else if (i === 19)
                        {
                            $w('#txtLoading').hide()
                            $w('#CommodusCheck').show()
                            $w('#CommodusX').hide()
                            $w('#Commodus').link  = link
                            $w('#Commodus').target = "_self";
                        }
                        else if (i === 20)
                        {
                            $w('#txtLoading').show()
                            $w('#PertinaxCheck').show()
                            $w('#PertinaxX').hide()
                            $w('#Pertinax').link  = link
                            $w('#Pertinax').target = "_self";
                            $w('#txtLoading').hide()
                            $w('#txtLoading').collapse()
                        }   
                        else if (i === 21)
                        {
                        //  $w('#txtLoading').hide()
                            $w('#DidiusJCheck').show()
                            $w('#DidiusJX').hide()
                            $w('#DidiusJ').link  = link
                            $w('#DidiusJ').target = "_self";
                            $w('#txtLoading').show()
                            $w('#txtLoading').expand()                          
                        }       
                        else if (i === 22)
                        {
                            $w('#txtLoading').hide()
                            $w('#SeptimiusSCheck').show()
                            $w('#SeptimiusSX').hide()
                            $w('#SeptimiusS').link  = link
                            $w('#SeptimiusS').target = "_self";
                        }                   
                        else if (i === 23)
                        {
                            $w('#txtLoading').show()
                            $w('#PNigerCheck').show()
                            $w('#PNigerX').hide()
                            $w('#PNiger').link  = link
                            $w('#PNiger').target = "_self";
                        }       
                        else if (i === 24)
                        {
                        //  $w('#txtLoading').show()
                            $w('#ClodiusCheck').show()
                            $w('#ClodiusX').hide()
                            $w('#Clodius').link  = link
                            $w('#Clodius').target = "_self";
                        }                                                                                                                               
                    }   
                } )             
        }
            $w('#txtLoading').hide()
            $w('#txtLoading').collapsed
            $w('#loading').show()
            $w('#loading').expand()
        allowClick = 1  
}

From the screenshots below the 2 POST errors , one is in checkTheBoxes() and the other is in MarkTheXs(). MarkTheXs() was the first red error in the console.

export async function MarkTheXs()
{
    //let userEmail = await wixUsers.currentUser.getEmail()
    let endEmp = AllEmperors.length 
    let results
    console.log('MarkX email=',useremail),'*'
        for (var i=0;i<endEmp+1;i++) { 
            console.log('endEmp=',endEmp)
            let results = await wixData.query('RomanImperialCoins').eq('user', useremail) 
                .eq('title', AllEmperors[i])
                .limit(1000)
                .ascending('title')
                .find()     
                .then((results) => 
                {
                    let numCoins = results.totalCount
                    console.log('i am here','numCoins=',numCoins)
                    if (numCoins === 0)
                    {
                        if (i === 1)
                        {
                            $w('#Vespasian').link  = ''
                            $w('#VespasianX').show()
                            console.log('VESP',i)
                        }
                        else if (i === 2)
                        {
                            $w('#Licinius').link  = ''
                            $w('#LiciniusX').show()
                            console.log('LIC',i)
                        }   
                        else if (i === 3)
                        {
                            $w('#Augustus').link  = ''
                            $w('#AugustusX').show()
                            console.log('AVG',i)
                        }           
                        else if (i === 4)
                        {
                            $w('#Tiberius').link  = ''
                            $w('#TiberiusX').show()
                            console.log('TIB',i)
                        }       
                        else if (i === 5)
                        {
                            $w('#Caligula').link  = ''
                            $w('#CaligulaX').show()
                            console.log('CALIG',i)
                        }
                        else if (i === 6)
                        {
                            $w('#Claudius').link  = ''
                            $w('#ClaudiusX').show()
                            console.log('CLAUD',i)
                        }   
                        else if (i === 7)
                        {
                            $w('#Nero').link  = ''
                            $w('#NeroX').show()
                            console.log('NERO',i)
                        }               
                        else if (i === 8)
                        {
                            $w('#Galba').link  = ''
                            $w('#GalbaX').show()
                            console.log('GALBA',i)
                        }                                                           
                        else if (i === 9)
                        {
                            $w('#Otho').link  = ''
                            $w('#OthoX').show()
                            console.log('OTHO',i)
                        }   
                        else if (i === 10)
                        {
                            $w('#Vitellius').link  = ''
                            $w('#VitelliusX').show()
                            console.log('VIT',i)
                        }           
                        else if (i === 11)
                        {
                            $w('#Titus').link  = ''
                            $w('#TitusX').show()
                            console.log('TITUS',i)
                        }       
                        else if (i === 12)
                        {
                            $w('#Domitian').link  = ''
                            $w('#DomitianX').show()
                            console.log('DOMIT',i)
                        }                           
                        else if (i === 13)
                        {
                            $w('#Nerva').link  = ''
                            $w('#NervaX').show()
                            console.log('NERVA',i)
                        }   
                        else if (i === 14)
                        {
                            $w('#Trajan').link  = ''
                            $w('#TrajanX').show()
                            console.log('TRAJAN',i)
                        }   
                        else if (i === 15)
                        {
                            $w('#Hadrian').link  = ''
                            $w('#HadrianX').show()
                            console.log('HADRIAN',i)
                        }           
                        else if (i === 16)
                        {
                            $w('#AntoninusPius').link  = ''
                            $w('#AntoninusPiusX').show()
                            console.log('AP',i)
                        }       
                        else if (i === 17)
                        {
                            $w('#MarcusAurelius').link  = ''
                            $w('#MarcusAureliusX').show()
                            console.log('MARCUS',i)
                        }   
                        else if (i === 18)
                        {
                            $w('#LuciusVerus').link  = ''
                            $w('#LuciusVerusX').show()
                            console.log('LUCVER',i)
                        }       
                        else if (i === 19)
                        {
                            $w('#Commodus').link  = ''
                            $w('#CommodusX').show()
                            console.log('COM',i)
                        }   
                        else if (i === 20)
                        {
                            $w('#Pertinax').link  = ''
                            $w('#PertinaxX').show()
                            console.log('PERT',i)
                        }   
                        else if (i === 21)
                        {
                            $w('#DidiusJ').link  = ''
                            $w('#DidiusJX').show()
                            console.log('DIDJ',i)
                        }
                        else if (i === 22)
                        {
                            $w('#SeptimiusS').link  = ''
                            $w('#SeptimiusSX').show()
                            console.log('SS',i)
                        }                                                                                           
                        else if (i === 23)
                        {
                            $w('#PNiger').link  = ''
                            $w('#PNigerX').show()
                            console.log('PesNiger',i)
                        }       
                        else if (i === 24)
                        {
                            $w('#Clodius').link  = ''
                            $w('#ClodiusX').show()
                            console.log('CLOD',i)
                        }   
                        console.log('i am here, no more',i)                                             
                    }   
                } )             
        }   
}

If this helps for clarity, A few screen shots, when you come into the page the top right drop down box is not populated, Then if I select a user, it does its thing, usually successful the first time, then flipping back and forth between the users, eventually (usually 2 or 3 times,) it breaks.

So, the most obvious error in your code is that you are using await and the .then() method to deal with the wixData.query() returned promise. You should use one or another, not both. I prefer for most cases to use async/await , but it is up to you.

There are better ways of dealing with multiple collections query requests, that is not the best one.

You are also creating two results variables, with different scopes each, wich it is quite confusing.

The other thing that is striking to me is that I think you could be using a repeater to populate these results, instead of manually dealing with each one of them.

I can create an abstraction to make this code more concise, but it is not the best approach, I think you have to rethink it around a repeater, and if you need any help, I’m here for it.

I’ll take a better look at your code when I get home.

@bwprado hey Bruno. Thanks for the quick look. I’m quote the novice here, I’m sure I wasn’t doing things optimally but does work. kind of. First time using wix for a project. I’ve not looked at repeaters. hopefully tonight I can look at what you suggest. Appreciate the feedback and help here!

Can you explain better what the query is doing? And can you describe your collection data?

The collection data consists of Ancient Coins associated with a specific Ruler, by a specific user on the site.

The query is identifying if the selected user’s coins has at least 1 of the rulers. If it is found, then it makes the checkmark image box visible and makes the X mark hidden. If the query finds no coins, then the X mark remains visible. If the user does have a coin of a specific ruler, then the image box links to another page, to display all of the coins from that ruler. In essence a checklist, of does the user have a coin from a specific ruler. The dropdown box in the top right selects which user to use for the query, as well as other pages on the site.

So lets say we have 2 users, which the upper right dropdown box allows you to select.
User1 has a coin of Augustus
User2 does not.

When user1 is selected in the dropdown, then the checkmark and link will work for the Augustus picture/description. For user2, no link will be set ( since there are no coins of augustus and the X mark will remain If you want to see it, if you’d like I can post the link to the site, although I didn’t want to share until I finished up more aspects of it :-). I did put some screen shots lower in this thread.

Thanks again! I still have to look at your suggestions regarding repeater, , haven’t had a chance yet. I played a little with just using ASYNC/AWAIT, or Then, and everything stopped working :-), but didn’t have a chance to evaluate what I needed to fix to continue that way.

Hey @bwprado , just an update, I don’t want you to be wasting more time they need to. You’re advice helped me get past the problem. I moved the query out of the loop. As well as removed the .eq(‘title’), and searched for that after I had the query results in an array. It worked, way better, and significantly faster, by not having the query in the loop!.


for(vari=0;i<endEmp+1;i++)
{
letresults=awaitwixData.query('RomanImperialCoins').eq('user',useremail).eq('title',AllEmperors[i]).limit(1000).ascending('title').find().then((results)=>{

.....

I am going to look at repeaters for another part of the page, the Feed, since most of the page hasn’t used them yet, I am certainly hesitant to muck around with it now that I got this working.

Here’s the page link if you’re interested.
https://vjbonan.wixsite.com/aetcol/empck

NOTE the remote site / mobile site is still busted, that’s a project for later! The desktop site is where it’s fixed.

Thanks again, for looking it over and suggesting switching stuff in the query, it helped me get on the right track! Cheers!