Calculating Fields and updating the database

Hello, i have a problem with a new project:
So, its a “ticketing” system, for a Bus transport company.
The image below is an admin page, and only the bus driver will have acces, so that he can issue a ticket.


The text below the image, is the passenger’s available ammount, and its being fetched by the database passengers , from the field key money and its being passed to the text field with id #text1 .
The 4 buttons at the bottom of the page are all ticket options,

  • 1.20€(1st button - with ID #b1)

  • 0.60€(2nd button - with ID #b2)

  • 1.70€(3rd button - with ID #b3)

  • 0.90€(4th button - with ID #b4)
    I have made if statements, to disable a button, if the available ammount is not enough, that’s why the 3d button is disabled.
    The table on the right are passenger’s old tickets.
    The field near the box with the image, is a “topup” option.The input field , with ID #topup accepts only numeric input.
    So my problem is that i can’t find out how to add and substract money from the avaialble balance:
    Once the admin clicks on a ticket button( e.g. if admin clicks button 1 - #b1, i need the available amount reduced by 1.20, so the value after the button is clicked should be 0.40 ), and then update the database for that change.
    And finally, for the “topup”, i wont integrate a payment system, it will be “cash”, the passenger will give the money to the bus driver and he will just add the money.
    So here i need the variable taken from the input field, being added to the available amount.So if 50 is the input, then the database value should be 51.60(50 plus the 1.60 from before).
    I just can’t find a way to do this…
    i can calculate the fields to a variable, but i dont know how to update the database…
    Can you please give me an example on one button how the code is done?

Look here

 function update(collectionName: string, item: Object, [options:WixDataOptions]): Promise<Object> 

i think update is needed here…but i cant find out exactly how it works, what it calls…
e.g. function update (collectionName: passengers, item: money)
what do i place in the:

Look at the sample

import wixData from ‘wix-data’;

// …

let toUpdate = {
“_id”: “00001”
“title”: “Mr.”,
“first_name”: “John”,
“last_name”: “Doe”
};

wixData.update(“myCollection”, toUpdate)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );

function topup() {
	console.log("Inside ToPup");
	var userid = $w('#text20').text;
	var balance = parseFloat($w('#balance').text);
	var input = parseFloat($w('#topup').value);
	var final = balance + input;
	let toUpdate = {
		"userid": userid,
		"money": final
	};console.log("ToUpdate");
	wixData.save("passengers", toUpdate)
		.then((results) => {
			let item = results; //see item below
		})
		.catch((err) => {
			let errorMsg = err;
		}); console.log("results" + final);

}

export function button1_click() {
topup();
}

console.logs are printing fine…all of them.so the code does run, without problems.
But, the database is not updated when the button is clicked.

it adds it as new entry

Correction – When i have wixData.Save it does that in the picture above(adding new enties)
In wixdata.update, it doesnt do anything…

in save new items are created, in update it will update but only if you specify what row to update using the _id field or what it is called in your collection. look in collection under hidden fields and show your id. then include your id field in the object when updatng your rows.


How will i get the _id though…i don’t see it in the dropdown, and i use the value of the dropdown to check what user is displayed on the dynamic page.

It seem you have an ID that is the key so use that if you want to update the row per ID

Yes - i have to use that, but how do i get the _id from the database?
I dont want a specific _id, to just search for it…
i want to get the _id that the current user has on the dynamic page.

You can get the _id field from the current item in the dataset.
Use this API, the item will contain an _id field.

Hi Tomer,
thanks for the answer.
Once i call this let itemObj = $w(“#myDataset”).getCurrentItem();
it will give me an itemObj and it will contain an array, which will contain a lot of info.How will i specificly the _id field of all theese info provided, save it on a var so then later i can put that variable on the update method?

I mean i first need to call the let itemObj = $w(“#myDataset”).getCurrentItem();
then i need to somehow isolate the _id from the itemObj in to a new variable, the variable id1 .
And after that i go on update
let toUpdate = {
“_id”: id1 ,
“money”: final

Replace id1 with itemObj._id

IT WORKED!!!
Thank you so much!!!

Hi, please I need your help. I have a form that when filled by my site member populates a database. The content of the database is displayed for each member on their profile based of what the filled in the form.

The problem is:
I have a number that is displayed on the member’s profile(also filled by the member while filling the form and stored in the database), I have a code that changes the value of the number on the member’s page but I am not able to update the number in the database and save it with the new value so that when the member logs-in again, the new value is displayed and can also be updated in the future. How do I do it? Thanks

Hi Chidi, Please create a new post with the details of your problem, and provide any relevant code, screenshots, etc. Posting guidelines are here . Thank you.