Reputation: 1788
I have a function that inserts into a database a random number in a column that is the primary key.
I have a problem when the number is already in the database I want to call the process again to change the number and try to insert again but I am a novice with Javascript so here is the code I have come up with so far.
Function that create a random number:
EDIT 2:
function crearNumero()
{
var aNumeros = new Array
(
'1', '2', '3','4','5','6','7','8','9'
);
var cNumero = aNumeros[Math.floor(Math.random()*aNumeros.length)];
return cNumero;
}
Function that inserts in to the database:
function insertarNumero(Callback)
{
var sql='INSERT INTO tabla_numero(numero) VALUES(?)';
var params= [crearNumero()];
client.query(sql,params,function(err,rows)
{
if(err){
arguments.callee(Callback);
} else {
return Callback(params[0]);
}
});
}
So I try to insert the number with this:
insertarNumero(function(args)
{
console.log('el numero ingresado fue '+args);
console.log('the number inserted was '+args);
});
This code is ok but when the number exists already I dont know how to repeat de process.
Can anyone give me an indication as to how I can go about solving this issue?
Upvotes: 1
Views: 2506
Reputation: 2993
You want to actually pass a callback function to the insertarNumero
function, like so:
function anotherInsert() {
insertarNumero(crearNumero(), anotherInsert);
}
This will cause the insertarNumero
function to re-call this function, should it fail. Be advised that you should probably also check the error code so that you don't attempt to insert multiple times, if there was a connection error, for example.
Edit: Seeing as you're still having difficulty resolving your issue, here are two alternatives which might help you:
function insertarNumero(callback) {
var sql = 'INSERT INTO tabla_numero(numero) VALUES(?)';
var params = [crearNumero()];
client.query(sql, params, function(err, rows) {
if (err) {
callback(err);
} else {
callback();
}
});
}
function insert() {
insertarNumero(function(err) {
if (err) {
console.log('failed to insert');
insert();
} else {
console.log('inserted');
}
});
}
And if you really don't want the insert()
function...
function insertarNumero(callback) {
var sql = 'INSERT INTO tabla_numero(numero) VALUES(?)';
var params = [crearNumero()];
client.query(sql, params, function(err, rows) {
if (!err) {
if (callback) {
callback(params[0]);
}
} else {
//TODO: Check the error code
insertarNumero(callback);
}
});
}
insertarNumero(function(args) {
console.log('the number inserted was ' + args);
});
Upvotes: 1
Reputation: 1714
One possibility is an auto increment instead of random numbers. This would be more comfortable.
If you really want to insert random numbers, you should create a unique index
on numero
. After calling your query, you could check if the query succeeded, if it did not, check whether it was because the number already existed. (Check the error number returned by the mysql server.) If it failed, you create a new random number and call your function again to insert the new number.
PS: At the moment, you could only store nine rows in your database.
Upvotes: 1
Reputation: 5263
Just call itself again and pass same Callback using arguments.callee
:
function insertarNumero(Callback)
{
var sql='INSERT INTO tabla_numero(numero) VALUES(?)';
var params= [crearNumero()];
client.query(sql,params,function(err,rows)
{
if(err){
arguments.callee(Callback);
} else {
return Callback();
}
});
}
arguments.callee
is same as using this function's name insertarNumero
Upvotes: -1