Problems with returning Wix Data by function

Hello,

I want to create a 1-time download code for a site and I have a problem. I can’t add a way for a not-found button to appear if the code is wrong. I want to make a function that returns a number based on if the code was found in the content manager in a collection. I am using a for(i=0; i < 21; i++) method to scan all the slots in my collection. If I add an (if i==21 and codefound=false) type of thing it then just ALWAYS says not found.

export function redeemButton_click ( event ) {

**function**  look (auth ID ) { 
    **let**  inputKey  =  $w ( '#inputCode' ) 
    **if**  ( inputKey . value  !=  "Empty"  &&  inputKey . value  !=  "" ) { 
    
**for**  ( **let**  i  =  0 ;  i  <  21 ;  i ++) { 
    
    wixData . query ( "Codes" ) 
. find () 
. then ( ( results ) => { 
    **if** ( results . items . length  >  0 ) { 
    **let**  curItem  =  results . items [ i ];  //see item below 
    console . log ( i ) 
    console . log ( curItem . value ) 
    
    **if**  ( curItem.value  ==  inputKey.value  && 
    curItem .auth id  ==  auth ID  
    

    ) { 

        $w ( '#redeemButton' ). label  =  "Redeemed!" 
        console . log ( "Found" ) 

        
        **if**  ( curItem . media  ==  "Video" ) { 
            curItem . value  =  "Empty" 
        wixData . update ( "Codes" ,  curItem ) 
            **return**  1 
            
        } 
        **if**  ( curItem . media  ==  "All" ) { 
           
            curItem . value  =  "Empty" 
        wixData . update ( "Codes" ,  curItem ) 
            **return**  2 
            
        } 
        **if**  ( curItem . media  ==  "Pictures" ) { 
            
            curItem . value  =  "Empty" 
        wixData . update ( "Codes" ,  curItem ) 
            **return**  3 
            
            } 
        
    } 
    
    
    } 
    }) 
    . **catch** ( ( err ) => { 
**let**  errorMsg  =  err ; 
console . log ( errorMsg ) 

} );

} 

} 
} 

// EDIT THIS PART!!!!! 
// ||||||||||||||||||| 
// VVVVVVVVVVVVVVVVVVV 
**let**  myAuthID  =  "L29me_i4d" 
// /\/\/\/\/\/\/\/\/\ 
// ||||||||||||||||||| 
// EDIT THIS PART!!!!! 

look ( myAuthID ) 
 // let code = await lookUp(myShowID, $w('#inputCode'), 0) 
 // await console.log(code) 

What I want is:

function search () {
let trialInputCode = $w ( ‘#inputCode’ ). value
let i = 0
for ( i = 0 ; i < 21 ; i ++) {
let myValue = lookUp ( myAuthId , trialInputCode , i )
if ( myValue == 0 && i == 21 ) {
console . log ( “Not Found!” )
}
if ( myValue == 1 ) {
//Do Whatever
return
}
if ( myValue == 2 ) {
//Do Whatever
return
}
if ( myValue == 3 ) {
//Do Whatever
return
}
}
}

It’s not so clear what you’re trying to do:

  1. read the docs https://www.wix.com/velo/reference/wix-data/query

  2. . please rewrite the code so the look function declaration won’t be inside export event handler.

  3. your query has no parameters, add them to the query.

  4. do not run the query 21 times, use .hasSome() to run it one time.

  5. If it still doesn’t work, post the the corrected code here in a code block

Ok, thanks so far! I changed for it to use the .hasSome() function and not run 21 times. Now the problem that I am having is returning a value that doesn’t work. I think I am just using async and await wrongly.

import { lookUp } from ‘backend/aModule.jsw’ ;

export async function button5_click ( event ) {
let myShowID = “Lib_2021_AliceIW_TheMonkeys”
let value = await lookUp ( myShowID , $w ( ‘#inputCode’ ). value )
await console . log ( await value )
}

aModule.jsw:

export async function lookUp ( showID , inputKey ) {
await wixData . query ( “Codes” )
. hasSome ( “value” , inputKey )
. find ()
. then ( ( results ) => {
if ( results . items . length > 0 ) {
let curItem = results . items [ 0 ]; //see item below
console . log ( curItem . value )

    **if**  ( curItem . value  ==  inputKey  && 
    curItem . showid  ==  showID  
    

    ) { 

        
        console . log ( "Found" ) 

        
        **if**  ( curItem . media  ==  "Video" ) { 
            
            curItem . value  =  "Empty" 
        wixData . update ( "Codes" ,  curItem ) 
            **return**  1 
            
        } 
        **if**  ( curItem . media  ==  "All" ) { 
            
            curItem . value  =  "Empty" 
        wixData . update ( "Codes" ,  curItem ) 
            **return**  2 
            
        } 
        **if**  ( curItem . media  ==  "Pictures" ) { 
            
            curItem . value  =  "Empty" 
        wixData . update ( "Codes" ,  curItem ) 
            **return**  3     
            
            } 
        
    } 
    
    
    } 
    **else** 
    { 
        console . log ( "Not Found!" ) 
        **return**  0 
    } 
    }) 
    . **catch** ( ( err ) => { 
**let**  errorMsg  =  err ; 
console . log ( errorMsg ) 

} );

}

It just returns “undefined”

@wynngeisler I thought you wanted to query for an array of values.
Anyway it’s not clear to me what you’re trying to do.

I am trying to make a 1 time download code for media. I am using content manager to store the codes and originally I had the script search each slot for the code but then I added the .hasSome() filter. Now I am using the if (results.items > 0) function.

@jonatandor35 I just know know that the problem is something with async and await. The function is in a backend script. I want it to return a value like 0, 1, 2, or 3 based on the query results.

@jonatandor35 Also, check the screenshot. That will explain more things.

Just some similar posts (additional ifos & ideas)…

https://russian-dima.wixsite.com/meinewebsite/oneway-ticket

https://russian-dima.wixsite.com/meinewebsite

Try to get useable info and convert & use it for your own purposes.