How to add UserId and Path before insert into Database?

Hi,

My visitors on the site click on a button to choose the designs they like, this gets inserted as a Boolean into a Database Collection “ChooseDesign”. I want to capture the visitor User Id and the path of the page where they have chosen design. Every design is a dynamic page.

I am trying to use before Insert hook. Here is the code snippet I am using on data.js of the hook function. The user input - boolean is getting inserted. But however I try the UserId and the path is always NULL. Can someone please help me?

//data.js

import function wixUsers from ‘wix-user’;
import function wixLocation from ‘wix-location’;

export function ChooseDesign_beforeInsert(item,context) {

let UserId = wixUsers.currentuser;
let DesignId = wixLocation.url;

item.User = UserId;
item.Design = DesignId;

return item;

}

Hi Sadiq,

behind the scene the data.js code is invoked on Wix servers as a serverless service, not on the browser, so it does not have the context of the page. it is very useful to enforce any logic regardless of the page/form/source of where the data was inserted to the database from.
wix location however is a cross browser abstraction on top of your browser url address, and therefore if you want to add additional data to your item before submit, you can use the following api which is executed in the browser - http://www.wix.com/code/reference/wix-dataset.html#onBeforeSave

good luck!
Shlomi

Thanks Shlomi, for the insights. Bingo! It’s working now.