@chocookie22
All the ways are fine and it really depends on the specific use case. but especially if you want to use insert it on different ages using different functions, I’d create an afterInsert Data Hook :
Like this:
//backend/data.js
import wixData from 'wix-data';
export function myCollection_afterInsert(item, context) {
item.clientId = item._id;
return wixData.update(context.collectionName, item, {suppressHook:true, suppressAuth: true}).then(() => item);
}
Alternatively I’d create my own record ID and then insert all at once:
//declare a createId() function to create a random uuid, you can can install the uuid npm for that
const item = {_id: createId()};
item.clientId = item._id;
wixData.insert('collectionName' itme);
or use a beforeInsertHook:
//backend/data.js
//declare a createId() function to create a random uuid, you can can install the uuid npm for that
import wixData from 'wix-data';
export function myCollection_beforeInsert(item, context) {
item._id = createId();
item.clientId = item._id;
return item;
}
And of course you can go with the code you posted (just add dataset.save(); no need to push this value to any UI element).
As I said, the best choice depends on the specific use case.
See more: