Subtract a number in database by current logged in member and upadte it to the database

Hello I want if any one can help me out… I tried many times but not getting successful so if any one can help me I will be glad. The page is show the particular points for each logged in members and those points are allotted to the member in a database. I’m trying to get onclick function on my button to subtract the point by 1 and the upadte the data I database.

For example if balance is show 365 it should be subtract by 1 and the database should be updated with 364 and if balance is 0 the button should not hover or have a text message you don’t have enough balance

Hello,

Step-I: Getting user-id or e-mail

import wixUsers from 'wix-users';
2
3// ...
4
5let user = wixUsers.currentUser;
6
7let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
8let isLoggedIn = user.loggedIn; // true
9
10user.getEmail()
11  .then( (email) => {
12    let userEmail = email;      // "user@something.com"
13  } );

Step-I: Getting the results of your DB by userID…

wixData.query("DATABASENAME")
   .eq("COLUMN-ID", userEmail)
6  .find()
7  .then( (results) => {
8    if(results.items.length > 0) {
9      let firstItem = results.items[0]; //see item below
10    } else {
11      //....do here your substruction
        let myNewPointValue = results.items[0].points-1
        console.log(myNewPointValue)
12    }
13  } )
14  .catch( (err) => {
15    let errorMsg = err;
16  } );

Step-III: Putting both steps together into one function.

Thank you @russian-dima I’ll try and let you know

Little correction…
Step-I: Getting the results of your DB by userID…or eMail…

.eq("COLUMN-ID", userId)
.eq("COLUMN-ID", userEmail)

It is not working out

I do not see any code of you :grin:
What are we talking about?

@russian-dima Sorry i didn’t get you?

You wrote …

It is not working out

What did you do? Did you work on the code?

@russian-dima can you help me in aligning the code on the page because I’m not able to do that

So you did not try to join the two CODE-PARTS together?
I wanted to see if you are really working on your own CODE, or if you just awaiting for a SOLUTION from me?

@russian-dima I’m not expert in coding and i don’t know much about coding I’m trying on youTube tutorials and watching them I would be happy if developer like you would help us in learning and get experience and lessons from you developers

If you want to learn, than YouTube is a good idea, and also take a look at all the example-tutorials on my site.
You will also find links to other EXPERTS like QC-Nayeli on my site.

There is so much stuff from which you can learn.

What you could already do by yourself is to prepare the code, that is all i expected from you, like this… (deleting all unnessasery code-parts, and give some structure to it…)

import wixUsers from 'wix-users';

$w.onReady(function () {    });

 let user = wixUsers.currentUser;

 let userId = user.id;     
 let isLoggedIn = user.loggedIn 

    user.getEmail()
    .then( (email) => {
 let userEmail = email;      
    });


    wixData.query("DATABASENAME")
    .eq("COLUMN-ID", userEmail)
    .find()
    .then( (results) => {
 if(results.items.length > 0) {
 let firstItem = results.items[0]; //see item below
        } else {
 //....do here your substruction
 let myNewPointValue = results.items[0].points-1
            console.log(myNewPointValue)
        }
    } )
    .catch( (err) => {
 let errorMsg = err;
});

This was it already. Prepared code to work with.

Now putting the 2 parts together and creating of it a function…(CODE-IMPROVEMENT)

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

$w.onReady(function () { myFunction() });  // This will start your function ---> "myFunction"

async function myFunction (parameter) {console.log("myFunction-started")
 //PART-I ----> GETTING USER-DETAILS WHO IS LOGGED IN.
 let user = await wixUsers.currentUser;
 let userId = await user.id;     
 let isLoggedIn = await user.loggedIn 

 await user.getEmail()
    .then(async (email) => {
 let userEmail = await email;      
 
 // PART-II ---> GETTING DATA-QUERY OF YOUR DATABASE.
        wixData.query("DATABASENAME")
        .eq("COLUMN-ID", userEmail)
        .find()
        .then( (results) => {
 if(results.items.length > 0) {
 let firstItem = results.items[0]; //see item below
            } else {
 //....do here your substruction
 let myNewPointValue = results.items[0].points-1
                console.log(myNewPointValue)
            }
        } )
        .catch( (err) => {
 let errorMsg = err;
        });
    });
}

That looks already better right?

Inserted some ASYNC-AWAIT in the first part, because the second part should not start before first part of code is done.

But STOP, wait! You still know what you want to do ???

Yes you want first to get the —> eMail of the logged in User right?
And then ?
Right! Then you filter your DATABASE by this eMail to find the right logged in USER (You could also use the USER-ID for that).

In this example i just will do it with the eMail.

  1. Got your eMail
  2. putted in into the filter
  3. Filtered your database by this founded eMail
  4. Found the right row in the right column —> Found your ITEM
  5. Now replace/change vlaue of that founded ITEM.

Replace in the code “COLUMN-ID”, by your own column-ID.
When i take a look at the bad-quality-pic you gave, i can recognize thet the related colum-ID is —> “email” ,right?

.eq("COLUMN-ID", userEmail)

changing to …

.eq("email", userEmail)

Now the code would search in the right column -->“email” for the value ----> “userEmail” and you get a RESULT after the filtering-process is done.

.then( (results) => {   ....here all the results of the filter...  }

Now try to understand till here. You are still not done with this code. It has still to be EXPANDED.

What does this code already?
Which part of code is still missing?
What has to be implemented into this code to solve your issue completely?

EDIT:
And of course do not forget to change the DATABASE-NAME…

wixData.query("DATABASENAME")

…to…your own one…

wixData.query("upload")

All is done by theoretical thoughts, i did not test it, so it could still be buggy.
But when you understand what you are doing, then you should be able to DEBUG all little errors & code-bugs.

Thank you @russian-dima i Will work on this and thankyou for your patience and such careful lesson

No problem! Work on it and when you get stuck again, you ask again. :wink:
And do not forget to like it, if you really liked it. :grin:

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

$w.onReady(function () { myFunction() });  // This will start your function ---> "myFunction"

async function myFunction (parameter) {console.log("myFunction-started")
 //PART-I ----> GETTING USER-DETAILS WHO IS LOGGED IN.
 let user = await wixUsers.currentUser;
 let userId = await user.id;     
 let isLoggedIn = await user.loggedIn 

 await user.getEmail()
    .then(async (email) => {
 let userEmail = await email;      
 
 // PART-II ---> GETTING DATA-QUERY OF YOUR DATABASE.
        wixData.query("upload")
        .eq("email", userEmail)
        .find()
        .then( (results) => {
 if(results.items.length > 0) {
 let firstItem = results.items[0]; //see item below
            } else {
 //....do here your substruction
 let myNewPointValue = results.items[0].points-1
                console.log(myNewPointValue)
            }
        } )
        .catch( (err) => {
 let errorMsg = err;
        });
    });
}

$w.onReady(function () {
let user = wixUsers.currentUser;
let userEmail;
user.getEmail().then( (email) => {userEmail = email;
$w("#dataset1").onReady(() => {
$w("#dataset1").setFilter(wixData.filter()
.eq("email", userEmail)
)
})
} );
});

hello @russian-dima i added this code but i guess we have to add onClick function to deduct when the button is clicked ??

I am sure you are able to solve this little problem on your own :wink:

There are 2-ways how to do it.

Here you will find both ways…
https://russian-dima.wixsite.com/meinewebsite/multistate-boxes