Hello, I am trying to add 300 to a field "credit" in my credit database but my codes seems not to be working.
import wixData from 'wix-data';
$w.onReady(function() {
$w("#button1133").onClick(function() {
wixData.query('credit').find().then(results => {
const record = results.items[0];
record.credit +=Number(300);
wixData.save('credit', record);
});
});
Any help will be appreciated
Look at using setFieldValue to save into a dataset field.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue
Also, this line will not work as it is.
$w("#button1133").onClick(function() {
Firstly button1133, you have over 1000+ buttons on your site, wow and good luck remembering where each and everyone one is exactly, you would be much better suited calling them something that refers to their action or use, so for example a submit button you could simply call it submitButton.
As for the line itself, you need to enter it on your page as either of the following two ways.
export function button1133_onclick(event) {
This is when you add the onClick event handler through the properties panel for the button itself. The line of code will be added automatically at the bottom of your current code and you will need to delete it if you have already used it, otherwise you will have duplicates.
$w("#button1133").onClick( (event) => {
This way of coding has the onClick event handler function already written into the code itself, so you do not need to use the onClick event handler from the button element's properties panel.
Also, with your query, what are you actually trying to find in your credit dataset?
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html
Typically, you build a query using the query() function, refine the query by chaining WixDataQuery functions, and then execute the query by chaining one of the following: find(), distinct(), or count()
For example, the following code queries a collection for all male customers over the age of 20 and logs the first 15 results to the console, sorted in ascending order by name:
import wixData from 'wix-data'; wixData.query("Customer") .gt("age", 20) .ascending("name") .limit(15) .find() .then( (results) => { console.log(results.items); } );