Wix Database question - Points between members

Hi Guys, Im trying to setup a database that allows me to send points between users within the database.

I can get it work with set values and deduct points for current user logged in however struggling to add points to the user being sent the points

I haven’t done coding for years so trying to find my feet

The page has a dropdown linked to a dataset and only shows current usernames with one selected by default

I have an input box for user to input points being sent which is used in code below

needless to say current code is not working :stuck_out_tongue: - any help is welcome lol

import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;
let user = wixUsers . currentUser ;
let points = 0
let sentpoints = 0
let userEmail = “user email”
let user1 //to save user1 points
let user2 //to save points being sent to user2

$w . onReady ( function () {
user . getEmail ()
. then ( ( email ) => {
userEmail = email ; //get current user email who is logged in
} );


export function button8_click ( event ) {

wixData . get ( “www-points” , userEmail ) // Find current user who is logged in
. then ( ( results ) => {
let item = results ; //see item below
points = item . wwwpoints ; // capture current points
$w ( “#input3” ). value = points ; // show current points on a read only text box
user1 = item ; // store table for use later
console . log ( user1 );
} )
. catch ( ( err ) => {
let errorMsg = err ;
} );

let amount = 0
amount = Number ( $w ( “#input4” ). value ) // capture users input as the amount
if ( amount < 0 ) {
$w ( “#input5” ). value = “Cannot Be Approved” // avoid negative values
else {

if (points - amount < 0 ) { // If user does not have enough points
$w ( “#input5” ). value = “Not Enough Points”
else {
points = points - amount // deduct points from current user
$w ( “#input3” ). value = points ; //show current points for user
user1 . wwwpoints = points ; // update table with new points
wixData . update ( “www-points” , user1 ); // update database with new points
console . log ( user1 );

wixData . get ( “www-points” , $w ( “#dropdown1” ). value ) //dropdown only showing usernames via dataset to find user points being sent to
. then ( ( results ) => {
let item = results ; //see item below
sentpoints = item . wwwpoints ; //capture current points to use with sent points
user2 = item ; // save table for use later
console . log ( user2 );
} )
. catch ( ( err ) => {
let errorMsg = err ;
} );

sentpoints = sentpoints + amount ; // update sentpoints for user being sent points
user2 . wwwpoints = sentpoints; //update table with new points total for user
wixData . update ( “www-points” , user2 ); // update database with new points
console . log ( user2 );

$w ( “#input5” ). value = “Points Approved” //update textbox to show points worked

OK SOO I can now see the problem thanks to console log messages. Its when I’m using the additional variables to store the database information for use later specifically:

User1 and User2

problematic lines:
user1 = item ;
user2 = item ;

If I don’t use them the code works fine

Anyone know if I’m using incorrect variable type to store the information? I’m sure I’ll get to the bottom of it eventually but the sooner the better

Turns out talking to yourself can really help LOL

Code is now all working

I had to redo it all over again

In the end I found that using

$w ( “#dataset1” ). getItems ( 0 , totalitems )

works better cause you can fill a variable ( user1, user2 ) with the entire table of items then use it to search what you want and update to the database using

wixData . update ( “www-points” , user1 );

The challenging bit was trying to use a For loop to do the search from the array.

Only one problem im still having is that the data misbehaves sometimes and doesnt update im guessing i need to add the onReady function somewhere but seems minor, Transferring points seems to be working like a charm

I also have a table on a separate page on the website that shows total number of points

Coding can be addictive

once I can figure out why my database is playing up, the code will be complete.

As always any feedback is welcome xD

Turns out talking to yourself can really help LOL
It’s not the talking to yourself, it’s the amount of effort and time you invest into your code.

Less invested time and effort —> quantity .
More invested time and effort —> quality .

And YES → CONSOLE-LOGS are your best friends!

Thanks Ninja, I intend to see it through till the end lol

You wouldn’t know a way I could delay these commands so they complete before going on to the next ones?

I believe the database doesn’t update sometimes causing problems with my current code.

wixData . update ( “www-points” , user1 );
wixData . update ( “www-points” , user2 );

when it works, it works flawlessly.

Its nice to get back a little into coding, reminded why I liked it soo much to begin with :slight_smile:

Take care when working with DATABASES, especially looking for commands like…

  • .then(()=>{ });
  • .onReady(()=>{ });

JS codes are working synchronous
Imagine you have 2x racing cars which drives exatcly at identical speed on a data-highway.
Left sided data-highway goes traight to the endpoint and has about 1000m.
Right-sided data-highway has to go around a big mountain and has a length of about 3000m.

So CAR-1 who is driving the left-sided data-highway, of course will arrive first at endpoint.

The same happens in JS-CODINGs when working with DATABASES.
A DATABASE must first be queried or saved.
A query or save-process needs → TIME (more meters to drive).

Now look again onto your code and try to find the right involved code-part.

Another option to await for something is → setTimeout(()=>{ }, 5000 ); 5000=5secs .
But this is just an alternative solution which is not really recommended, WHY???

Because not every PC or INTERNET-CONNECTION has the same speed.

  1. for PC-1 the setted TIME-OUT can be enough
  2. but perhaps for PC2 it will not be enough of waiting time.

RESULT → CODE-crushes or throws errors, due to → SYNCHRONITY.

And now back to → ASYNC-AWAIT! → Setting from SYNCHRONITY to → ASYNCHRONITY! :wink:

And at the end → you now know why using → setTimeOut is → Bullshit :sweat_smile: and is not recommend.

Wow thanks for that, Really helps understand. The issue I was having was due to not resetting a Boolean variable I was using. Since I fixed that code the database was updating like I wanted with no issues xD.

I have hit another minor snag though :((

I tested my page with multiple users signed up and logged in ( helpful friends ) and even though code works when only ONE person is logged in, it doesn’t seem to like concurrent users logged in at the same time.

I guess what I need is each member to open up an instance of this page rather than use the same page, since the variables being used get assigned incorrect users points when all are logged in at the same time.

Nothing is as easy as it seems in the end LOL when i started off on this project 2 weeks ago I didn’t think I’d end up learning soo much.

I have a problem though, I cant stop doing something until I get the end result I want.

Have to read up on how to create page per user, that might help fix this issue but since this is just an idea lets see where this takes me lol