I found this code that generates random alphanumeric codes, I am using it to link 2 types of users, the user can only generate the code once and save it in a table with id and code, then that code is given to other users so that they register it and see some things together.
I want to know how I implement that the codes that are being saved in the bd are not repeated, for the moment I bring them from a database with ajax and they are in an array, the generator has it in a function so that it does not it remains front only is so that they see more or less how it works
var caracteres = "abcdefghijkmnpqrtuvwxyzABCDEFGHJKMNPQRTUVWXYZ2346789";
var contraseña = "";
for (i=0; i<20; i++) contraseña +=caracteres.charAt(Math.floor(Math.random()*caracteres.length));
console.log(contraseña)