query and filter- what is wrong with my code

I don’t understand how is that the output for this code is:
final filter is:

if in my code the first output is “coreNameFound” and only then “final filter is”.
May be query method is async? So what is the “wait” method?
Please help!!!

export function buildFilter () {

// $w(“#datasetFields”).onReady( () => {
var filter = wixData.filter();
var coreName = “”;
//filter by fields category

      filter = filter.eq('fieldCategory', $w('#dropdownFieldCategory').value); 

                .eq('_id', $w("#dynamicDataset").getCurrentItem("coreEvent").coreEvent) 
                .then(results => { 

let items = results.items;
let item = items[0];
coreName = item[“coreEventName”];


if (coreName.length>0)
console.log(“add to filter:”+coreName);
filter = filter.eq(‘coreEvent’, coreName);
console.log(“final filter is:”);
//finally, set the filter

First of all the getCurrentItem is to be used like below according to API Docs.

let itemObj = $w("#myDataset").getCurrentItem();
let coreEventId = itemObj.coreEvent;
// Then execute query
  .eq('_id', coreEventId)
  .then((results) => {
    if (results.totalCount > 0) {
      // Here you get results
      coreName = results.items[0].coreEventName; // Use this 
      console.log("add to filter:"+coreName);           
      filter = filter.eq('coreEvent', coreName);
    } else {
      // Here you don't get results
    console.log("final filter is:");         
    console.log(filter);  // finally, set the filter           

So it is important to stay within the results promise to make sure you get results and that you then can use. Also I made some changes in how to set the names and how to get the coreEventId from the current item in your Dynamic Page.

1 Like