Problem with returning function + limit + skip

Doing this way → NO PROBLEM! Everything works!

function zzz(skipValue, limitValue) {
    wixData.query("DATABASE")
    .skip(skipValue)
    .limit(limitValue)
    .find()
    .then((res)=>{let items = res.items; 
        console.log("ITEMS: ", items);
        $w('#rep3').data = items;
    });
}

Trying to do this way → FAILS! What am i missing ???

let limitValue = 10;
let skipValue = 20;

let data = await get_AllProductsData(limitValue, skipValue);

async function get_AllProductsData(limitValue, skipValue) {    
    let query =  await wixData.query("DATABASE");     
    if(limitValue) {query.limit(limitValue);}
    if(skipValue) {query.skip(skipValue);}
    return query.find()    
    .then(async(res)=>{//console.log("RES-PRODUCTS: ", res);
        if (res.items.length > 0) {console.log("Data found!");
            let items = res.items; //console.log("ITEMS: ", items);
            return (items);
        }
        else {console.log("No data found!"); return ([]);}
    }).catch((err)=>{console.log(err); return [];});
}

Also tried the following…

let limitValue = 10;
let skipValue = 20;

let data = await get_AllProductsData(limitValue, skipValue);

async function get_AllProductsData(limitValue, skipValue) {    
    let query =  await wixData.query("DATABASE");     
    if(limitValue) {await query.limit(limitValue);}
    if(skipValue) {await query.skip(skipValue);}
    return query.find()    
    .then(async(res)=>{//console.log("RES-PRODUCTS: ", res);
        if (res.items.length > 0) {console.log("Data found!");
            let items = res.items; //console.log("ITEMS: ", items);
            return (items);
        }
        else {console.log("No data found!"); return ([]);}
    }).catch((err)=>{console.log(err); return [];});
}

…and also tried this one …

let limitValue = 10;
let skipValue = 20;

let data = await get_AllProductsData(limitValue, skipValue);

async function get_AllProductsData(limitValue, skipValue) {    
    let query = await wixData.query("DATABASE");     
    if(limitValue) {return query.limit(limitValue);}
    if(skipValue) {return query.skip(skipValue);}
    return query.find()    
    .then(async(res)=>{//console.log("RES-PRODUCTS: ", res);
        if (res.items.length > 0) {console.log("Data found!");
            let items = res.items; //console.log("ITEMS: ", items);
            return (items);
        }
        else {console.log("No data found!"); return ([]);}
    }).catch((err)=>{console.log(err); return [];});
}

Can not find the right key for this issue :sweat_smile:

A query is built by chaining. As stated in the documentation : “…you build a query using the query() function, refine the query by chaining WixDataQuery functions…”. As far as I know, it doesn’t work the same way as building a filter for a Dataset setFilter() .

I’ve also wanted to do something like this. Would be a nice feature to add to the Wishlist.

@yisrael-wix
Damn! So my idea is not possible to be realized. Damn!:sweat_smile::sleepy::sleepy::sleepy::sleepy:

So i have to go the odd and boring way :sleepy::sleepy::sleepy::sleepy::man_facepalming:.

Ideed this would be a very useful upgrade for wix-data-api.

But what about this one…?
This example works like a charm!

As far as I know, it doesn’t work the same way as building a filter for a Dataset setFilter() .
Or did i missunderstood your comment?
Is this one, a DATASET-one?

async function get_specificData(DATABASE, FIELD1, VALUE1, FIELD2, VALUE2, FIELD3, VALUE3, UniqueField, STATE, FIELD4, ARRAY1){
    let query = wixData.query(DATABASE);  
    if(FIELD1) query = query.eq(FIELD1, VALUE1)
    if(FIELD2) query = query.eq(FIELD2, VALUE2)
    if(FIELD3) query = query.eq(FIELD3, VALUE3)
    if(FIELD4) query = query.hasSome(FIELD4, ARRAY1)
    return query.find()
    .then(async(res)=>{//console.log("RES: ", res);
        let items = res.items; //console.log("Items: ", items);
        let uniqueData = await get_UniqueData(items, UniqueField)
        
        if (STATE===true) {let uniqueData = await get_UniqueData(items, UniqueField);} //console.log("Unique-Categories: ", uniqueData);}

        // This function will give you back --> UNIQUE-ITEMS...
        async function get_UniqueData(items, dbField){
            const categories = await items.map(item=>item[dbField]);
            const uniqueItems = await [... new Set(categories)]; //console.log("Unique-Items: ", uniqueItems);
            const uniqueOptions = [];
            
            for (let i = 0; i < uniqueItems.length; i++) {
                const item = uniqueItems[i];
                uniqueOptions.push({_id: i.toString(), label: item, value:item});      
            }
            return ({uniqueItems: uniqueItems, uniqueOptions: uniqueOptions});   
        }
        return ({items: items, uniqueData: uniqueData});
    }).catch((err)=>{console.log(err); return [];});
}

I am using this to avoid numerious different maden Query-functions.

This multi-function replaces several single ones, with build-in features.

Of course this one is just an example (i have even more complex ones, which works this way).


At the END → i found a DIRTY-WORKAROUND for a returning function, which lets my compact CODE → explode!

async function get_AllProductsData(limitValue, skipValue) {    
    if(limitValue && skipValue){
      let query =  wixData.query(DATABASE); 
      return query.limit(limitValue).skip(skipValue).find()
      .then(async(res)=>{let items = await res.items; console.log("ITEMS: ", items);
          if (items.length > 0) {console.log("Data found!"); 
              return (items);
          }
          else {console.log("No data found!"); return ([]);}
      }).catch((err)=>{console.log(err); return [];});    
    }
    
    else{let query =  wixData.query(DATABASE); return query.find()
        .then(async(res)=>{let items = await res.items; console.log(ITEMS: ", items);
            if (items.length > 0) {console.log("Data found!"); 
                return (items);
            }
            else {console.log("No data found!"); return ([]);}
        }).catch((err)=>{console.log(err); return [];});    
    }
}

…just because of a little missing function-feature :disappointed_relieved::persevere::disappointed_relieved::persevere::disappointed_relieved::persevere::disappointed_relieved::persevere:

After days of code-experiments and code-investigations, i got my working workaround, which makes the chaining-function seemingly possible without to make the code exploding in size.

So i would say - → It doesn’t need to be placed onto the wishlist.
There is a workaround for this issue, which makes it possible to chain everything :grin:.

But this solution is not for public. Waiting for → some new features on the Editor.

And at the end → my wished functionality was possible :hugs: