using a value from a collection

i want to get the highest value from a column in my collection
i want to make a calculation on it and bring it back to the collection in a new column.
i tried to use the get() function but it is looks that it return a value that I am all ready know.
i will love if someone explain me how to do it, thanks from advance :).
Dor.

to find the highest value:
wixData.query(“myCollection”) .descending(“last_name”) .find() .then( (results) => { let items = results.items; let firstItem = items[0];

first item will have the highest value.
the use “update” to update the record after you modify the field that you want (for example items[0].last_name). note that the wixData.update must be insie the “.then” as the query is asynchronous. for more info see: wix-data - Velo API Reference - Wix.com

hi erezsh
i understood how to get the highest value from the column in the collection, but i didn’t understand how and where to use the update() function, the example from the wix API is not clear to me.

I did not try it but it should look something like that:

wixData.query(“myCollection”)
.descending(“last_name”)
.find()
.then( (results) => {
let items = results.items;
console.log(items[0]);//write to console the old first item
//modify first item
items[0].xxx=yyy - put you modifications here
//write to collation updated item
wixData.update(“myCollection”, items[0])
.then( (results) => {
let item = results;
console.log(item); //write to console the new item
} )
.catch( (err) => { let errorMsg = err; } );
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
} );

to view console: F12
Hope it helps…

Hi,

In order to update an existing record, you should call the update function:

wixData.query("myCollection") 
	.descending("last_name") 
	.find() 
	.then( (results) => { 
		let items = results.items; 
		wixData.update("myCollection", items[0]) 
		.then( (result) => { 
			let item = result;
			//write here what you would like to update 
		} ) 
		.catch( (err) => { let errorMsg = err; } ); 
	})
	.catch( (error) => {
			let errorMsg = error.message;
			let code = error.code;
	} );

Click here to read more about the query function.

I hope that it’s clear.

Best,
Tal.

I don’t think so…update needs object item as input, not only id (" _id property of the specifieditem "). In my example id should already be the correct member of items[0]. but again, I did not check so I might be wrong…

I have written that code when someone new submit my web site.
i want that the new value that enters the kingsFortune column will be bigger by 1 then the highest value in the kingsFortune column cells that already exist.
and some of the code is working, its insert the new upperTitle item but not the new kingsFortune item.
what i did wrong?
i would like if some one will write me the correct code, and explain so in the future i will be able to write that kind of code by myself, thanks from advance:).

your errors are:
line 8 should be:
items[0].kingsFortune = items[0].kingsFortune +1;
line 12: return items[0];
You also have a conceptual error. You must do the update inside the “then” as it is async. Otherwise the function will return before “items” has its value. I suggest you start with just copying the example I provided as-is (only change the name of the fields).
Also note that in line 8 “kingsFortune” should correspond to the ID of the kingsForture column in your collection (the are not the same). Click the 3 points on the table header to find out the ID of the field (as opposed to its name).

hi erezs.
i write your code but it dont work.
what is wrong?