Need help making matchmaking queue

Hey guys, so bascially we are making a website that randomly allows strangers to match up (duos or squads) and then displays their matches on the screen. We wrote some code for the match making part of the website, however, it does not display the matches. We think the problem is the “people” variable because every time the page is opened by a new user, a new instance is created (and we think this is what is happening not sure). Thanks in advance!! :slight_smile:


import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
 
 var people = [];
    $w('#button6').onClick((event) => 
    {
         wixData.query("Members/PrivateMembersData")
        .eq("_id", wixUsers.currentUser.id)
        .find()
        .then((results) => {
 
        var first = results.items[0].firstName;
        var last = results.items[0].lastName;
 
        var team = makeTeam(first, last);
        if (team.length === 2){
                   displayTeam(team);
                   people.slice(0, 2);
           }
        else{
                $w("#text1").text = "Not enough people online to form team :(";
                $w("#text2").text = "Member 1";
                $w("#text3").text = "Member 2";
            }
 
        } )  
 
     });
 function makeTeam(first, last){
 
        people.push([first, last]);
 
        return people;
    }

 function displayTeam(team){
 
        $w("#text1").text = "Your team is:  ";
        $w("#text2").text = team[0][0] + ' ' + team[0][1];
        $w("#text3").text = team[1][0] + ' ' + team[1][1];
 
    }
 

});

Hello Vrund Patel,

take a look at this one…

https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields

Hey russian-dima, we added that part in to get the user’s first and last name. However, when two users click the button it does not match the two players together (bascially dosen’t put them on the same team).

For example, when I click the button it displays my name on the screen but the second person (who would be my teammate) does not appear.
And if I happen to click the button twice, both sloths get filled with my name (basically matching me with myself).

Hopefully I explained it properly,
Thanks! :slight_smile:

Hello again, your code is working, but there are two things which are not present in your code.

For example i tried to test yout CODE. I mentioned that this will not be possible to test your CODE in the right way, because everytime i do a log-in with a new user —> taht means → PAGE-REFRESH the results of the first founded person will be deleted.

What i want to say is —> you have to save your TeamPlayer in a DATABASE.
So you will have a function, which will save the PLAYERs into the DATABASE by clicking your button and a second function, which will get the data out of the DATABASE, when a user enters the page to look if new PLAYER are in the DATABASE.

The idea to use an ARRAY is ok, but you do not store/save the results.

You perhaps also could use the Wix-Storage…
https://www.wix.com/corvid/reference/wix-storage#top
…but better you use a DATABASE.

And the second problem is, that a user just will see the results when he enters the page. So you should also refresh the current results by cliking the button.
Then the user will see after his click on the button the actual-refreshed results (getting data from databse).