using a function with chaAT in backend datacollection hook

i have a hook on a data collection for before insert.
i call a function

function toUpperFirst(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}

found this exactly as in cose reference doc

when i call it in the before insert as follows

export function ClubMembers_beforeInsert(item, context) {
item.firstName = toUpperFirst(item.firstname);
item.surname = toUpperFirst(item.surname);
item.emergencycontact = toUpperFirst(item.emergencycontact);
item.relationship = toUpperFirst(item.relationship);
item.suburb = toUpperFirst(item.suburb);
return item

}

i get an error
save operation failed: TypeError: Cannot read property ‘charAt’ of undefined

You are probably using the incorrect field name:
item.firstName = toUpperFirst(item.firstname);
You should probably be using:
item.firstName = toUpperFirst(item.firstName);

Note, that you want to use firstName , not firstname.