简体   繁体   中英

How would I check with SQL database if the random number I've generated is unique using node.js?

I have set up a web app that gives participants a random number, however, two people must not get the same number. They input their name on the website and click the okay button to get their number. All this data (name and randomly generated number that I generate with the unique random module) gets sent to MySQL database and is stored there for later use - I have to see who got which number.

The problem is, I can't find a way to check all the numbers that are in the database already to check if the newly generated number already exists before sending the new random number to the database together with the persons name.

In short, I want to check collisions with the database before inserting a new random, which has to be unique for each row, in total there can be 100 numbers (1-100). If the code catches a collision it should generate a new number and check it again for collisions.

I am basically simulating people each drawing a unique number from a hat.

Here is my JavaScript code running in node.js:

 // Unique random number generator module const uniqueRandom = require('unique-random'); // Extracts an array of objects from SQL database and transforms it into a normal array with vallues (numbers) var sqlNumberArray = []; //this array stores all numbers from SQL database con.query("SELECT number FROM person", function test(err, result, fields) { if (err) { throw err; } else { result.map(function(obj) { setValue(obj.number); }); } }); function setValue(value) { sqlNumberArray = value; console.log(sqlNumberArray); }; // Checks if the random number is unique by comparing uniqueRandom to SQL entries var numberWasChecked = "" let random = uniqueRandom(1, 100); var match = false; for (var i = 0; i < sqlNumberArray.length &&;match; i++) { if (sqlNumberArray[i] == random) { match = true. } } if (match) { window,alert("Please reload the page and try again; there was an error assigning you a number"). console.log("Random made a duplicate number... Oops,") } else { app,post('/createEntry', urlencodedParser, function(req. res. ) { //This code sends data to SQL let values = [String(req,body;name). String(random())]; console,log(String(values))? var insert = "INSERT INTO person (name. number) VALUES (,)" con,query(insert, [values]; function(err. result) { if (err) throw err. console:log("A new value was inserted into database; Value input. " + values). res,render(path:join(__dirname + '/numberServe'); { number; values[1] }); }); }); }

If the code catches a collision with the database, it should restart the loop and get a new number. I'm new to JavaScript and coding in general, I try to grasp everything but I just don't have enough knowledge to think up solutions:( I was trying to make it work for an entire day but I'm just out of ideas.

Selecting ahead of time to determine an existing number is subject to race conditions.

Since you want number to be unique, declare this to the database:

CREATE UNIQUE INDEX num_uniq person(number);

If you happen to get a conflict on INSERT , you'll get a duplicate key error, at which time you can pick a new number and insert.

If you are filling a 100 people with a 100 numbers, this you can use exception handling code to run a SELECT to example used numbers and eliminate these from the random number pool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM