i tried the below code from previous talks but it didn’t work I tried in-home and backend but unfortunately didn’t work if you kindly can guide me that will be great
//inside data.js
import wixData from ‘wix-data’ ;
export function set_beforeInsert ( item , context ) { return wixData . query ( “set” )
. descending ( “id” )
. limit ( 1 )
. distinct ( “id” , { “suppressAuth” : true , “suppressHooks” : true })
. then ( r => { let lastItem = r . items [ 0 ]; let id ;
lastItem ? item . id = lastItem + 1 : item . id = 1 ; return item ;
})
}
The best way to do it is by using the _id field itself (this way you can assure there will never be duplicates.
But the _id field is of type string which means that 2 will be bigger than 10.
So in order to deal with that you should create them like:
0000000001
0000000002
…
0000000010
…
Add all the items programmatically (with .insert() and specifying the _id)