Button event with dataset

Hello and thanks for reading this,

I’m completely new to JavaScript and this is one of the only things I need coded on my site, but how do I make it so that when a button is pressed (I know how to specify the button), a certain bit of data is changed for someone.

More background: I have a website that there is a currency only on the website, it’s name is “Stars”. It’s similar to Robux from www.roblox.com . I want to make it so that when a user clicks a button, it removes a certain amount of stars from their account. So if I want to “buy” something from a shop in my website with Stars, and let’s say that the item costs 20 stars and I have enough stars to buy it, I need it so that it successfully subtracts 20 stars from their total amount of stars once they click the button.

To help your answer: The dataset’s name is “Stars” and the field is “Amount”.


Above: Yellow is dataset name, dark blue is field name, and red is the amount of stars the user has.
Disregard the UnlockedBackgrounds and Background field, they have nothing to do with this.

Thanks!

Your SETUP:

  1. You have a —> DATABASE
  2. You have a —> DATAFIELD
  3. You have a —> VALUE
  4. You have a —> USER (User-ID)

Project-Flow:

  1. Click on a Button (BUY).
  2. Changing VALUE in one of specific DATAFIELDS of a DATABASE.
  3. Related to a specific USER.

The only important thing what was not mentioned is —> if you are using a dataset ?

I would prefer the non-dataset-way (using wix-data-query).
The following CODE will find the right logged-in-User’s → AMOUNT !

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

$w.onReady(async()=>{
  let user = await wixUsers.currentUser;
  let userId = await user.id; 
  myFunction(userId);
});

function myFunction(ID){
  let DATABASE = "Stars"
  let DATAFIELD = "_owner"
  let VALUE = ID
  
   wixData.query(DATABASE)
   .find()
   .eq(DATAFIELD,VALUE)
   .then( (results) => {
     if(results.items.length > 0) {console.log("ITEMS: "; results.items)
       let firstItem = results.items[0]; console.log("FIRST-ITEM: ", firstItem)
       let amount = results.items[0].amount; console.log("AMOUNT: ", amount)
       
       //What to do next, after the right AMOUNT was found?
       //Continue here, now it''s your turn....
       //....your code 
       //...countinue
       //..here
       //.
       //
       
     } else {  }
   })
   .catch((err) => {let errorMsg = err;});
 }
 
 
//OUTPUT-EXAMPLE:
//------------------- 
/*  firstItem is:
 *
 *  {
 *    "_id":          "00001",
 *    "_owner":       "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
 *    "_createdDate": "2017-05-24T12:33:18.938Z",
 *    "_updatedDate": "2017-05-24T12:33:18.938Z",
 *    "title":        "Mr.",
 *    "first_name":   "John",
 *    "last_name":    "Doe"
 *  }
 */

You can use the CONSOLE to show all RESULTS.

In google-chrome —> use F-12 and navigate to —> CONSOLE.

Good luck…