Please help

The data is charged in 20 seconds and its too much time for a query, how can i impove the code, also when i enter the page1 doestnt run the query.

import {local} from ‘wix-storage’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
import wixUsers from ‘wix-users’;

var roleName;
let currentUser = wixUsers.currentUser;
currentUser.getRoles()
.then( (roles) => {
let firstRole = roles[0];
roleName = firstRole.name;
} );

export function searchBox_keyPress() {
search();
}
export function page1_viewportEnter(event) {
search()
}
export function searchButton_click() {
search();
}
export function searchBox_change() {
search();
}
export function dropdown1_change() {
search();
}
export function input1_keyPress() {
search();
}
export function input1_change() {
search();
}
export function resultsTable_rowSelect(event, $w) {
let rowData = event.rowData;
let rowIndex = event.rowIndex;
const myRow = event.target.rows[rowIndex];
wixLocation.to(${myRow["link-invitados-nombre"]})
}

function search() {
if (roleName===“Ho”) {
wixData.query(‘invitados’)
.contains(‘dni’, $w(“#searchBox”).value)
.or(wixData.query(‘invitados’).contains(‘nombre’, $w(“#searchBox”).value))
.or(wixData.query(‘invitados’).contains(‘apellido1’, $w(“#searchBox”).value))
.and(wixData.query(‘invitados’).contains(‘evento’,“Ho”))
.find()
.then(res => {
$w(‘#resultsTable’).rows = res.items; // see below
})
}
if (roleName===“Ri”) {
wixData.query(‘invitados’)
.contains(‘dni’, $w(“#searchBox”).value)
.or(wixData.query(‘invitados’).contains(‘nombre’, $w(“#searchBox”).value))
.or(wixData.query(‘invitados’).contains(‘apellido1’, $w(“#searchBox”).value))
.and(wixData.query(‘invitados’).contains(‘evento’,“Ri”))
.find()
.then(res => {
$w(‘#resultsTable’).rows = res.items; // see below
})
}
if (roleName===“Co”) {
wixData.query(‘invitados’)
.contains(‘dni’, $w(“#searchBox”).value)
.or(wixData.query(‘invitados’).contains(‘nombre’, $w(“#searchBox”).value))
.or(wixData.query(‘invitados’).contains(‘apellido1’, $w(“#searchBox”).value))
.and(wixData.query(‘invitados’).contains(‘evento’,“Co”))
.find()
.then(res => {
$w(‘#resultsTable’).rows = res.items; // see below
})
}
if (roleName===“Ki”) {
wixData.query(‘invitados’)
.contains(‘dni’, $w(“#searchBox”).value)
.or(wixData.query(‘invitados’).contains(‘nombre’, $w(“#searchBox”).value))
.or(wixData.query(‘invitados’).contains(‘apellido1’, $w(“#searchBox”).value))
.and(wixData.query(‘invitados’).contains(‘evento’,“Ki”))
.find()
.then(res => {
$w(‘#resultsTable’).rows = res.items; // see below
})
}
if (roleName===“Zo”) {
wixData.query(‘invitados’)
.contains(‘dni’, $w(“#searchBox”).value)
.or(wixData.query(‘invitados’).contains(‘nombre’, $w(“#searchBox”).value))
.or(wixData.query(‘invitados’).contains(‘apellido1’, $w(“#searchBox”).value))
.and(wixData.query(‘invitados’).contains(‘evento’,“Zo”))
.find()
.then(res => {
$w(‘#resultsTable’).rows = res.items; // see below
})
}
if (roleName===“Conc”) {
$w(‘#input1’).show()

  wixData.query('invitados') 
  .contains('dni', $w("#searchBox").value) 
  .or(wixData.query('invitados').contains('nombre', $w("#searchBox").value)) 
 .or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value)) 
   .and(wixData.query('invitados').contains('evento', $w("#input1").value)) 
  .find() 
  .then(res => { 
     $w('#resultsTable').rows = res.items; // see below 
  })  

}
}

$w.onReady(function () {
$w(“#resultsTable”).columns = [
{
“id”: “DNI”,
“dataPath”: “dni”,
“label”: “DNI”,
“width”: 100,
“type”: “text”,
},
{
“id”: “Nombre”, // ID of the column for code purposes
// The field key in the collection whose data this column displays
“dataPath”: “nombre”,
“label”: “Nombre”, // The column header
“width”: 100, // Column width
“type”: “text”, // Data type for the column
},
{
“id”: “Apellido”, // ID of the column for code purposes
// The field key in the collection whose data this column displays
“dataPath”: “apellido1”,
“label”: “Apellido”, // The column header
“width”: 100, // Column width
“type”: “text”, // Data type for the column
},
{
“id”: “Apellido 2”, // ID of the column for code purposes
// The field key in the collection whose data this column displays
“dataPath”: “apellido2”,
“label”: “Apellido 2”, // The column header
“width”: 100, // Column width
“type”: “text”, // Data type for the column
},
{
“id”: “Edad”, // ID of the column for code purposes
// The field key in the collection whose data this column displays
“dataPath”: “fechaNacimiento”,
“label”: “Edad”, // The column header
“width”: 100, // Column width
“type”: “date”, // Data type for the column
},
{
“id”: “Abono”, // ID of the column for code purposes
// The field key in the collection whose data this column displays
“dataPath”: “Abono”,
“label”: “Abono”, // The column header
“width”: 100, // Column width
“type”: “text”, // Data type for the column
},
];
});

Ok, let’s do it.

Some comments related to your code.

  1. Please use CODE-Tags/Snipets to show code in your post like this…

import {local} from 'wix-storage';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';

var roleName, currentUser

//-----START------------------
$w.onReady(function () {
    currentUser = wixUsers.currentUser;
    currentUser.getRoles()
    .then( (roles) => {
        let firstRole = roles[0];
        console.log(firstRole)
        roleName = firstRole.name; 
        console.log(roleName)
    });

    $w("#resultsTable").columns = [
        {"id": "DNI", "dataPath": "dni", "label": "DNI", "width": 100, "type": "text"},
        {"id": "Nombre", "dataPath": "nombre", "label": "Nombre", "width": 100, "type": "text"},   
        {"id": "Apellido", "dataPath": "apellido1", "label": "Apellido", "width": 100, "type": "text"},
        {"id": "Apellido 2", "dataPath": "apellido2", "label": "Apellido 2", "width": 100, "type": "text"},   
        {"id": "Edad", "dataPath": "fechaNacimiento", "label": "Edad", "width": 100, "type": "date"},
        {"id": "Abono", "dataPath": "Abono", "label": "Abono", "width": 100, "type": "text"},  
    ];


 //--- Button-Events/Actions ------------------
    $w('#searchBox').onKeypress(()=>    {search();})
    $w('#input1').onKeypress(()=>       {search();})
    $w('#page1').onViewportEnter(()=>   {search();})
    $w('#searchButton').onClick(()=>    {search();})
    $w('#searchBox').onChange(()=>      {search();})
    $w('#dropdown1').onChange(()=>      {search();})
    $w('#input1').onChange(()=>         {search();})
    $w('#').onKeypress(()=              {search();})
});


//--- Search/Filter-Function ---------------------------
function search() {
 let myQuery = wixData.query('invitados')

 if (roleName==="Ho") {
 //  wixData.query('invitados')
        myQuery
        .contains('dni', $w("#searchBox").value)
        .or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
        .or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
        .and(wixData.query('invitados').contains('evento',"Ho"))
        .find()
        .then(res => {
            $w('#resultsTable').rows = res.items; // see below
        }) 
   }

 if (roleName==="Ri") {
 //  wixData.query('invitados')
        myQuery
        .contains('dni', $w("#searchBox").value)
        .or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
        .or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
        .and(wixData.query('invitados').contains('evento',"Ri"))
        .find()
        .then(res => {
            $w('#resultsTable').rows = res.items; // see below
        }) 
   }

 if (roleName==="Co") {
 //  wixData.query('invitados')
        myQuery
        .contains('dni', $w("#searchBox").value)
        .or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
        .or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
        .and(wixData.query('invitados').contains('evento',"Co"))
        .find()
        .then(res => {
            $w('#resultsTable').rows = res.items; // see below
        }) 
   }

 if (roleName==="Ki") {
 //  wixData.query('invitados')
        myQuery
        .contains('dni', $w("#searchBox").value)
        .or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
        .or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
        .and(wixData.query('invitados').contains('evento',"Ki"))
        .find()
        .then(res => {
         $w('#resultsTable').rows = res.items; // see below
      }) 
   }

 if (roleName==="Zo") {
 //  wixData.query('invitados')
        myQuery
        .contains('dni', $w("#searchBox").value)
        .or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
        .or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
        .and(wixData.query('invitados').contains('evento',"Zo"))
        .find()
        .then(res => {
            $w('#resultsTable').rows = res.items; // see below
        }) 
   }

 if (roleName==="Conc") {
        $w('#input1').show()
 
 //  wixData.query('invitados')
        myQuery
        .contains('dni', $w("#searchBox").value)
        .or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
        .or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
        .and(wixData.query('invitados').contains('evento', $w("#input1").value))
        .find()

        .then(res => {
            $w('#resultsTable').rows = res.items; // see below
        }) 
    }
}


export function resultsTable_rowSelect(event, $w) {
 let rowData = event.rowData;
 let rowIndex = event.rowIndex;
 const myRow = event.target.rows[rowIndex];
    wixLocation.to(`${myRow["link-invitados-nombre"]}`)
}
  1. You do not have to query everytime again and again your collection (see code-improvement).

  2. Try to code more the —> “onReady-Style” instead of using “export-functions”!

  3. This code can be modified even much more.

it still have the problem, when i enter the page it doesnt show the data