0

I see that someone has given me a minus 1. I am a 55 year old mother who has no experience. I have many skills but this is not one of them. I am absolutely desperate and have bust myself to get this far. If you cannot help, I accept that, but please do not be negative towards me. I am now crying. Some encouragement would be much appreciated. I have a page which displays items from a database on a repeater. The code searches the items using several drop down filters, which are populated from the database. Intermittently, seemingly randomly (no pattern is emerging despite extensive testing) the code is failing to populate random drop down filters (one or more of the drop down filters show the default settings rather than those self populated from the database). I discovered this by either repeatedly visiting the page or by repeatedly refreshing the page. Often the code works, then every 3 or 4 times, one or more of the drop down filters shows its default settings rather than those self populated from the database (then the next time it goes wrong, it might be the same or a different one or set of filters which do not work) This is the code. On this page, there are 3 drop down filters but I have several pages like this, each displaying and searching a different database, with up to 10 drop down filters on each page, and they all have this intermittent problem...

import wixData from "wix-data"; 

$w.onReady(function () {    
    $w('#iTitle') 
    $w('#iCounty')      
    $w('#iGeog')      
    $w('#dataset1')       
    $w('#text102')      
});

let lastFilterTitle;        
let lastFilterCounty;
let lastFilterGeog;

export function iTitle_change(event, $w) { 
    filter($w('#iTitle').value, lastFilterCounty, lastFilterGeog);
}

export function iCounty_change(event, $w) { 
    filter(lastFilterTitle, $w('#iCounty').value, lastFilterGeog);
}

export function iGeog_change(event, $w) { 
    filter(lastFilterTitle, lastFilterCounty, $w('#iGeog').value);
}

function filter(title, county, geog) {        
    if (lastFilterTitle !== title || lastFilterCounty !== county || lastFilterGeog !== geog) {            
        let newFilter = wixData.filter();       
        if (title)
            newFilter = newFilter.eq('title', title); 
        if (county)
            newFilter = newFilter.eq('county', county);
        if (geog)
            newFilter = newFilter.eq('geog', geog);
        $w('#dataset1').setFilter(newFilter)
                .then(() => {                
                 if ($w('#dataset1').getTotalCount() ===0) {
                    $w('#text102').show();
                 }
                 else {
                     $w('#text102').hide();                     
                 }
                })
                .catch((err) => {
                   console.log(err);
                });
        lastFilterTitle = title;
        lastFilterCounty = county;
        lastFilterGeog = geog;        
    }
}

    // Run a query that returns all the items in the collection
    wixData.query("Psychologists")                                               
        // Get the max possible results from the query 
        .limit(1000)
        .ascending("title")
        .distinct("title")
        .then(results => {
           let distinctList = buildOptions(results.items);
           // unshift() is like push(), but it prepends an item at the beginning of an array
           distinctList.unshift({ "value": '', "label": 'All Psychologists'});
           //Call the function that builds the options list from the unique titles
           $w("#iTitle").options = distinctList
        });

function buildOptions(items) {
    return items.map(curr => {
        //Use the map method to build the options list in the format {label:uniqueTitle, valueuniqueTitle}
        return { label: curr, value: curr };
    })
}

    // Run a query that returns all the items in the collection
    wixData.query("Psychologists")                                               
        // Get the max possible results from the query 
        .limit(1000)
        .ascending("county")
        .distinct("county")
        .then(results => {
           let distinctList = buildOptions(results.items);
           // unshift() is like push(), but it prepends an item at the beginning of an array
           distinctList.unshift({ "value": '', "label": 'All Counties'});
           //Call the function that builds the options list from the unique titles
           $w("#iCounty").options = distinctList
        });

function buildOptions1(items) {
    return items.map(curr => {
        //Use the map method to build the options list in the format {label:uniqueTitle1, valueuniqueTitle1}
        return { label: curr, value: curr };
    })
}

    // Run a query that returns all the items in the collection
    wixData.query("Psychologists")                                               
        // Get the max possible results from the query 
        .limit(1000)
        .ascending("geog")
        .distinct("geog")
        .then(results => {
           let distinctList = buildOptions(results.items);
           // unshift() is like push(), but it prepends an item at the beginning of an array
           distinctList.unshift({ "value": '', "label": 'All Regions'});
           //Call the function that builds the options list from the unique titles
           $w("#iGeog").options = distinctList
        });

function buildOptions2(items) {
    return items.map(curr => {
        //Use the map method to build the options list in the format {label:uniqueTitle2, valueuniqueTitle2}
        return { label: curr, value: curr };
    })
}

export function button45_click(event, $w) {
    //Add your code for this event here: 
   filter($w('#iTitle').value='', $w('#iCounty').value='', $w('#iGeog').value='');
}

My experience and knowledge is very limited, so the answer may well be very simple. Any help would be much appreciated as I will have to abandon my project if I can't find a solution.Thank you

Entropy06
  • 11
  • 1
  • Do you understand how to use your browser's Developer Tools to check for script errors, debug your code and watch the results of network requests? That would be the place to go to start gathering clues – ADyson Nov 12 '19 at 12:50
  • Also, for us to have a better chance of helping you, it would be very useful to see the HTML which this code is interacting with :-) – ADyson Nov 12 '19 at 12:51
  • 1
    I'm not a wix code expert, but at first glance there doesn't seem to be anything wrong with the code you posted. Can you add a `console.log(results.items);` to every query, to see if the code is executed but returns unexpected results, or that the code isn't even executed at all? – Jeroen Nov 12 '19 at 12:54
  • 1
    What could be possible is that the event handler for `onChange` isn't fired for some reason. Have you tried adding an `onChange` handler in the `onReady`? Something like `$w('#iTitle').onChange((event) => { console.log('iTitle changed to ' + event.target.value); });`. With the same for the `iCounty` and `iGeog`. And then see what happens. – Jeroen Nov 12 '19 at 13:10
  • @Entropy06 Have you been able to solve your issue? – Jeroen Nov 20 '19 at 09:13

0 Answers0