Get index of item in dataset

Hi,

I created my own gamified waitlist with referrals and am trying to get the position of the item in the dataset to show the user their position in the queue.

Works like this
User signs up > if does not exist > add to waitlist (check if referral code associated with signup)
> if user exists > retrieve position in waitlist (and total number of invites)

When someone signs up using some user’s referral code, then that user should go up by 1 in the waitlist.

I’m handling the moving up in the waitlist automatically by sorting the dataset based on some criteria. I keep an invite counter that starts at 0 and every time someone signs up with a code, the respective inviter count goes up by 1 and so forth.

Now, I have everything done, except being able to retrieve the user’s index in the sorted dataset. I want the index so that I can get the position automatically without having to run back-end routine code.

I search for the respective user through
wixData . query ( “Waitlist” )
. eq ( ‘type’ , ‘Waitlist’ )
. count ()
. then ( ( waitlistCount ) => {

          wixData . query ( "Waitlist" ). eq ( 'email' ,  email ). find ( options ) 
	  . then ( ( results ) => { 
	 IF USER EXISTS THEN DISPLAY POSITION 
	  ... 
	  } 

}

I tried
getCurrentItemIndex, but always defaults to 0 because I search for the user and will always return either a 0 or a 1.
findIndex() was giving me a result that was not what I was expecting.

Any thoughts?

Thanks!

Where did you try to get the dataset index? In the code you shared you are performing a data query directly on the collection which, as you noted, would not know about the dataset index.

I’m accessing the dataset through front-end code, directly from my Wix website. Only doing queries.

The only other alternative I can think of is to iterate through the entire dataset until I find the matching email, but this would not be the most optimized method.

Should I iterate through my sorted list until I find the match and display the resulting counter?

Below is a portion of my code

// From Query to Position
$w ( “#button32” ). onClick (() => {

wixData . query ( "Waitlist" ) 
. eq ( 'type' ,  'Waitlist' ) 
. count () 
. then ( ( waitlistCount ) => {      //return total number of people on waitlist 

	console . log ( 'People registered '  +  waitlistCount ); 

	**let**  email  =  $w ( "#input13" ). value . toUpperCase ();       //to upper to match 
	**let**  refURL  =  wixLocation . query ;          
	console . log ( email ); 
	
	wixData . query ( "Waitlist" ) 
	. eq ( 'email' ,  email ) 
	. find ( options ) 
	. then ( ( results ) => { 

		**if** ( results . items . length  >  0 ) { 
			console . log ( 'User exists' ); //only one email allowed, if found result = 1 
			**let**  items  =  results . items ; 
			**let**  refID  =  items [ 0 ]. referralCode ;  //referral code to display 
			**let**  position  =  items [ 0 ]. position ;  //manual position I'm doing temp. 
			**let**  friendsReferred  =  items [ 0 ]. totalReferrals ;   //sum of friends ref. 
			**let**  referralLink  =  '[MY WEBSITE]?refID='  +  refID . toString () 
			$w ( "#text117" ). text  =  referralLink ;  //display URL with referral code 
			$w ( "#text120" ). text  =  position . toString () +  "/"  +  waitlistCount ; //position out of total people in waitlist 
			$w ( "#text118" ). text  =  friendsReferred . toString (); //num of friends that have referred and joined 
			$w ( '#EmptyFullStateBox' ). changeState ( "Position" ); //display the results in a different state 
		} 

		**else**  { 
			console . log ( 'User does not exist' );      //if does not exist, show error and prompt user to sign up instead 
			$w ( "#text124" ). show (); 
		} 

	} ); 
} ); 

} );

I could be wrong here, but I think you are confusing wixData API (which is querying the collection directly) and Dataset which is an element you add to your editor that serves as a connector to the database.

When you use wixData.query() you are querying the database table directly not the dataset element which is where your index from sort should be.

Have you looked at the dataset API ?

Overview of data and dataset

Thanks Amanda.

One clarification, perhaps what I’m missing is that from what I’m reading, usually the getCurrentItemIndex is triggered on event handlers (i.e. using repeaters). Which is not my case as I am not showing the list of all individuals on the waitlist, rather an input box for the user to type in the email, and then I match that email in the database to obtain the user’s ID (“_id”). Now I was hoping to pass that _id to get the index of the user in the dataset, but I’m limited only to getCurrentItemIndex.

So if there is no possible way of obtaining it that way, then I must revert to iterating through the list.

@lmvf19 Ah, I see what you mean. You need something like a getIndexById() which I am not seeing in the Dataset API unfortunately. Please read through yourself as well as you know your use case best, but it does sound like you may be correct unless someone else that sees this has a more efficient solution in mind. I will definitely keep thinking on it too and let you know if I come up with something.

Thanks @amandam ; to bring closure on this thread.

I could not find a shortcut way of doing it (eg. getIndexById() ); so I did it programmatically directly on the database.

My code is here for everyone to see for future reference. I resorted to running a back-end code routinely which would sort the database, iterate through it, and update everyone’s position based on the sorted criteria. It did the trick and will work for small waitlists.

export function sortWaitlist ( ) {

wixData . query ( "Waitlist" ) 
  . ascending ( "totalSum" ) 
  . descending ( "totalReferrals" ) 
  . ascending ( "_createdDate" ) 
  . eq ( 'type' ,  'Waitlist' ) 
  . find () 
  . then ( ( results ) => { 

  **let**  count  =  results . totalCount ; 
  **let**  items  =  results . items ; 

  **for** ( **var**  i  =  0 ;  i  <  count ;  i ++){ 

    **let**  toUpdate  = { 
      '_id' :  items [ i ]. idUpdate , 
      'email' :  items [ i ]. email , 
      'firstName' :  items [ i ]. firstName , 
      'lastName' :  items [ i ]. lastName , 
      'position' :  i  + + 1 , 
      'referralCode' :  items [ i ]. referralCode , 
      'referral' :  items [ i ]. referral , 
      'type' :  'Waitlist' , 
      'totalReferrals' :  items [ i ]. totalReferrals , 
      'totalSum' :  items [ i ]. totalSum , 
    } 

    wixData.update("Waitlist",toUpdate); 

    } 

})  // End getItems 

} // End For Loop

Thank you for providing the solution! Nothing worse than finding a thread with no resolution. I’m glad you were able to find a way to make this work. It’s a really interesting problem.