I have created a form with various fields (connected to a dataset within a data collection). When a user clicks submit all the data is stored in that collection.
However I wish to create a unique ID if any user submits the data. Can someone help with such a code?
I need it to be different unique ID to the automatically generated system unique ID.
I have followed this thread , but am having no luck at all! I just dont know what I am doing wrong! ? Id be so grateful for some help! I am not a natural coder
Do you have any code currently??
If so, paste it hereâŚ
I took this code from a previous thread but hasnât made a differenceâŚ
//inside data.js
import wixData from âwix-dataâ ;
export function pec200_beforeInsert ( item, context ) {
return wixData . query ( âPec200â )
. descending ( âuniqueIdâ )
. limit ( 1 )
. distinct ( âuniqueIdâ , { âsuppressAuthâ : true , âsuppressHooksâ : true })
. then ( r => {
let lastItem = r . items [ 0 ];
let newId ;
lastItem ? newId = lastItem + 1 : newId = 1 ;
return item ;
})
}
i wondered if I needed to do anything to the dataset Fields ? Shall I screen shot the pec200 datasheet ?
Hi @gergemina and @ajithkrr ⌠
You can do this with a data Hook ( beforeInsert ).
import wixData from 'wix-data';
export async function collection_beforeInsert(item) {
let id, unique;
while (!id && !unique) {
id: String(Number(Math.floor(Math.random() * 10000000000)));
let count = await wixData.query('col').eq('id', id).find().then((result) => {
return result.totalCount;
})
if (count > 0) { item.id = id; unique = true; }
}
return item;
}
Hope this helps~!
Ahmad
1 Like
Thanks for this, sadly it hasnât worked, unless I am missing something?
This code must be placed on the backend JavaScript file (data.js).
Iâve updated the answer, please try again now and let me know how it goes.
Yes, I inserted it there. However it did not recognise âcolâ. My collection and dataset is called PEC200. The following does not work for me
import wixData from âwix-dataâ ;
export async function PEC200_beforeInsert ( item ) {
let id , unique ;
while ( ! id && ! unique ) {
id : String ( Number ( Math . floor ( Math . random () * 10000000000 )));
let count = await wixData . query ( 'col' ). eq ( '_id' , id ). find (). then (( result ) => {
return result . totalCount ;
})
if ( count > 0 ) { item . _id = id }
}
return item ;
}
âcolâ is the collection/database name, in your case it must be replaced with " PEC200".
Also, update your code with my most recent updated answer above.
thanks, do I need to create a field in my database to allow for the new autogenerated ID?
Yes, it needs to called âidâ for the code to work, or just replace the âidâ in the orange part in my answer (item. id ) to your field key.
Should it be text field or number field?
A text field, also note that if you want to change the field key, you need to change it both from the query and from the value assignment. (Notice the orange parts).
Im so grateful for all your help so far but it still isnâ
t working. Here is a screen shot of my code, also my dataset, as well as my form.
1 Like
Also unfortunately entering this code has meant that the form on my live site does not work at all. When I removed the code it started working again⌠I was wondering if you could identify whats going wrong here?
Maybe because you havenât published the site after adding this code. Try it and let me know how it goes.
@gergemina did you find a solution? I have the exact same questionâŚ
Yes I did, I created a data.js file in the backend
and then used this code:
//inside data.js
import wixData from âwix-dataâ ;
export function PEC200_beforeInsert ( item , context ) {
return wixData . query ( âPEC200â )
. descending ( âidâ )
. limit ( 1 )
. distinct ( âidâ , { âsuppressAuthâ : true , âsuppressHooksâ : true })
. then ( r => {
let lastItem = r . items [ 0 ];
let id ;
lastItem ? item . id = lastItem + 200 : item . id = 1 ;
return item ;
})
}