Limiting user inputs based on CurrentMemberOrders

Hi all,

I’m very new to Velo, so apologies if I’m missing something very basic!

Members of my website will be on 1 of 3 paid membership options (multiples allowed), each giving them the right to record 1, 5 or 10 items in a database/collection.

I want to prevent users recording more than the total number of collection entries they’ve paid for.

My code is below. Can you spot where I’m going wrong?

Cheers,
Sam

import { currentMember } from ‘wix-members’ ;
import wixData from ‘wix-data’
import wixPaidPlans from ‘wix-paid-plans’ ;

$w . onReady ( function () {
// Write your JavaScript here

// To select an element by ID use: $w('#elementID') 

// Click 'Preview' to run your code 

currentMember . getMember () 
    . then (( member ) => { 
        **const**  id  =  member . _id ; 
        **const**  curMemEmail  =  member . loginEmail ; 
        **return**  member ; 
    }) 
    . **catch** (( error ) => { 
        console . error ( error ); 
    }) 

wixPaidPlans . getCurrentMemberOrders () 
    . then (( orders ) => { 
        **let**  PlanCap  =  0 
         **for**  ( **let**  i  =  0 ;  i  <  orders . length ;  i ++) { 
            **let**  item  =  orders [ i ]; 
            **if**  ( item . planName  ===  "Plan Value 1"  &&  item . status  ===  "ACTIVE" ) { 
                PlanCap  =  PlanCap  + 1 
            }  **else**  { 
            **if**  ( item . planName  ===  "Plan Value 5"  &&  item . status  ===  "ACTIVE" ) { 
                PlanCap  =  PlanCap  + 5 
            }  **else**  { 
                **if**  ( item . planName  ===  "Plan Value 10"  &&  item . status  ===  "ACTIVE" ) { 
                PlanCap  =  PlanCap  + 10 
        
                }}}}} 


$w ( "#EventsData" ). onReady ( () => { 
    console . log ( "The dataset is ready" ); 
    
    wixData . query ( "#EventsData" ) 
        . eq ( "Email" , "CurMemEmail" ) 
        . count () 
        . then ( ( num ) => { 
            **let**  numberOfItems  =  num ; 
            **if**  numberOfItems  >  PlanCap { 
                $w ( "#newEventButton" ). disable (); 
            } ) 
        . **catch** ( ( error ) => { 
            **let**  errorMsg  =  error . message ; 
            **let**  code  =  error . code ; 
        } ); 

}
})

You have a number of issues with your code…

The variable curMemEmail is not available later in your code since it is only defined (and available) in the .then() function of currentMember . getMember ().

The same goes for the variable PlanCap which is only available in the .then() function of wixPaidPlans . getCurrentMemberOrders ().

The .then() function is triggered when the called function’s Promise is resolved. See the following for more information about Promises:

In your database query, you see to be mixing datasets and collections together. What is #EventsData? Is it a dataset or is it a database collection? If it is a collection, then you don’t need the .onReady() function, and you should be getting an error. If it’s a dataset, then wixDataQuery won’t work since wixDataQuery only works with collection.

If #EventsData is a collection, then you also have an error in your query:

.eq("Email","CurMemEmail")

The field key should be used and always starts with a lowercase letter. You should also be using a variable without quotes. Like this:

.eq("email",CurMemEmail)

Note: as I mentioned above, the variable CurMemEmail won’t be available here.

Refer to the documentation for Wix Data with Velo for more information.