How to update dataset using JS?

I want to update a field in a dataset when the user presses a button.

did you check out the dataset API page?

Thanks, but I want to increment the value by 1. Sorry for not stating this above.

just use the API of the dataset to get the value, increment it by 1, and set it back to the dataset.
you can then use the save() function to persist the information to the collection.

It would be helpful if you could share the code of that operation. Thanks a lot!

hint:
look at these APIs:

Thanks for the help Ziv!

I would look at the reviews example, they do increase and store back. There you have all code you will need fo rthis.

I got this code but it doesn’t seem to work, can anybody tell me why?



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

$w.onReady(function () {
	//TODO: write your page related code here...
	const currentUser = wixUsers.currentUser;
	
	$w('#dataset4').onReady(() => {
		$w(this).setFilter(wixData.filter().eq('_owner', currentUser.id));
	});
	$w("#dataset1").onAfterSave(function () {
		//TODO:  update 'reports' field in users dataset
		console.log("Scores are updated!");
		let reports = $w("#dataset4").getCurrentItem().reports;
		console.log("current reports:" + reports);
		$w('#dataset4').setFieldValue("reports", reports + 1);
	});
});

CONSOLE:

Don’t use after shave, just take away that function and and add the .save() function on the collection at the end after setfieldvalue. After save is a hook that runs after you have executes save function.

It also seems you don’t have correct permissions on data collection. Right click on collection and choose permission add update permission for everyone.

Andreas - what’s the problem with after shave? it smells real nice!
:wink:

Balaram - it would be helpful if you could give more info on the exact usecase you have and the components on the page.
the code you pasted has several issues but in order to guide you we need more information.

thanks!

So the user is supposed to report a score and the scores then written to the Scores Dataset, and when this is successful, I want to update the user’s profile by incrementing his number of reports by 1 [reports++]. I hope this helps! Thanks for your help Ziv!

So Mr AutoCorrect is back here with After Shave on today :slight_smile:

You have some form element where user enters a score and that score data is saved in #dataset1 so after that data is saved you use the OnAfterSave function and want to update another collection with the incremented value, correct I hope?

So you maybe just need to add the save method to actually save the data. But also make sure the permissions are correct. I have done several mistakes setting the wrong permissions.

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

$w.onReady(function () {
	//TODO: write your page related code here...
	const currentUser = wixUsers.currentUser;
	
	$w('#dataset4').onReady(() => {
		$w(this).setFilter(wixData.filter().eq('_owner', currentUser.id));
	});
	$w("#dataset1").onAfterSave(function () {
		//TODO:  update 'reports' field in users dataset
		console.log("Scores are updated!");
		let reports = $w("#dataset4").getCurrentItem().reports;
		console.log("current reports:" + reports);
		$w('#dataset4').setFieldValue("reports", reports + 1);
    		$w('#dataset4').save();
	});
});

However, the error “save operation failed: TypeError: Cannot read property ‘reports’ of null” is still coming do you know why?

What do you get if you console.log(reports); ?

In the preview I don’t get anything, other than the error and “Scores are updated!” However, when I publish the site and viewed the console on chrome and this came [I reported 3 scores back to back]:


Over here it seems the reports value actually incremented itself, but in the Live Database, the reports field remains 0. Is this glitch or coding problem, I don’t know.

Permissions?

These are the permissions for the users collection (dataset4)

Hello Balaram,

The error “Cannot read property ‘trim’ of undefined” is given because you are trying to use $w by passing “this” as a component selector which is not supported.
You should replace $w(this) with $w(’ #dataset4 ').
We are working on giving a more meaningful error message in such cases :slight_smile:

The error “Cannot read property ‘reports’ of null” is given probably because the code “$w(” #dataset4 “).getCurrentItem()” returns null which may happen if the dataset does not point to any record (empty database, filter that does not match any record, etc…).
Your code should ensure that the user indeed has a related record in “dataset4” and if not, create one.

Regarding the issue of seeing the report value incremented during preview but not in the database - did you make sure to add the call to $w(“#dataset4”).save() as Andreas suggested ?