SOLVED: Using Hooks to insert current user ID to form submisson

Hello everybody,

i am looking to add the userID of the currently logged in user to the data which is sent to a database (called Stories) after a form submission. I tried to use a beforeInsert Hook that adds the ID into a database field called userReference.

The value of the userReference field is always null before the hook; i am trying to give the initial information through the hook.

The solution is in the comments down below!

Greetings, Daud

Hello from the Wix DevRel Team!

I think the issue has to do with the fact that you’re calling the wixData . insert ( “Stories” , toInsert ) function inside of your hook. Have you tried moving that outside of your Stories_beforeInsert() function and calling it there?

Hope this helps!


Hey, thanks for the help! Appreciate it!

You mean outside of the function or outside of the whole backend file? If its still in the backend file i don´t really know how to edit the code.

@acteevent I think it can technically be called in either place, but would make more sense to call in the same backend file. If you’re using the code in our examples and haven’t done a ton of complex workarounds, then the Stories_beforeInsert function should only be called just before the .insert() function is called. Because they’re nested in your case, the Stories_beforeInsert is not being called.

Hope this helps!


@roba82587 I see. Well i tried to move the line outside of the function but it´s still messed up.

import wixData from ‘wix-data’ ;
import wixUsers from ‘wix-users’ ;

export function Stories_beforeInsert ( item , context ) {

**if** ( item . userReference  ==  0 ){ 
    referenceInsert ; 
**return**  item ; 


let toInsert = {
‘userReference’ : wixUsers . currentUser . id ,

function referenceInsert ( ) {
wixData . insert ( “Stories” , toInsert )

Did you try invoking the referenceInsert function inside of your if statement?

if (item.userReference === 0) {

@roba82587 Yes, it’s still not working. Do I have to add something in the frontend, like initiate something when the submit button is clicked?

You shouldn’t necessarily have to invoke it on the front end, but it might be helpful to try invoking inside of a file that’s not on the back end based on some other interaction that you know is already firing. A bit difficult for me to explain since I don’t know much about what the rest of your site looks like, but let me know if there’s anything else I can do to help!


Thank you for all the feedback.
I am basically trying to find out how much and what kind of information i can send to a database through a form submission and what needs to be coded. I also experienced that multireference fields in the databases are probably not accessible with form elements like dopdown menus or checkboxes; meaning i didnt find a way to connect the elements to a multireference field.

I actually figured it out after a while. I made the mistake to work with wixUsers instead of wixUsersBackend. From there it was pretty simple. I just added this line to the backend file:

import wixUsersBackend from ‘wix-users-backend’ ;

export function Stories_beforeInsert ( item , context ) {
item . userReference = wixUsersBackend . currentUser . id ;
return item ;


Thanks again for your help :slight_smile: