i wanted to know the best way for generating random numbers for invoice and it shouldn't be duplicated .. in my project i'm posting invoice number for each user into API and ofc i should completely avoid duplication ... is there a way to do this in Vue js?
below i tried this code but i believe it might duplicate by chance :\
const Random = '0' Math.ceil(Math.random()*1000000) moment(new Date()).format('DDMMYY');
CodePudding user response:
As time cannot be repeated, you can use new Date().getTime()
CodePudding user response:
Use UUID v4
- Install and import UUID
import { v4 as uuidv4 } from 'uuid';
- Set invoice ID
invoice.id = uuidv4(); // ⇨ '14cd6bcd-bbfd-4a2d-3b5d-ab8dfcbd4dea'
- At the backend side, don't forget to check that the invoice id is not already used.
- Best way is to define a database unique key.
