I have gotten my code to work (finally) for an auto-incrementing, text, unique ID. I have accomplished this by emplacing the following code in the backend data.js:
import wixData from ‘wix-data’ ;
export function SubjectNew_beforeInsert( item , context ) {
return wixData . query ( ‘SubjectNew’ )
. descending ( ‘newid’ )
. limit ( 1 )
. distinct ( ‘newid’ , {“suppressAuth” : true , “suppressHooks” : true })
. then ( r => {
let lastItem = r . items [ 0 ];
let newid ; lastItem ? item . newid = lastItem + 1 : item . newid = 1 ;
item . title = “FIG001-TX-2022-” + item . newid ;
return item ;
})
}
I would like to add another hook that converts the users data input to proper-case using the toupperFirst() function before it inserts the data into the dataset. I have tried to follow the “beforeinsert()“ example from Wix, and others, but have had no success.
My setup is as follows:
Collection ID: SubjectNew
Fields to be converted to upperFirst within SubjectNew before insert: sublna, subfna and submna
FieldID’s on form: sublna, subfna and submna
Currently I have tried to emplace the following code everywhere within the code above; never can get it to work:
export function SubjectNew_beforeInsert(item, context) {
let hookContext = context;
// convert to proper case
item.sublna = toUpperFirst(item.sublna);
item.subfna = toUpperFirst(item.subfna);
item.submna = toUpperFirst(item.submna);
return item;
}
function toUpperFirst(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
Any help would be appreciated.
Thanks!