Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:20:48+00:00 2026-06-08T10:20:48+00:00

I have a function that inserts into a database a random number in a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:20:50+00:00Added an answer on June 8, 2026 at 10:20 am

    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);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that inserts data into a SQLite database. I can manually
I have a function that inserts a chunk of data into oracle database. I'm
Let's say I have a function that inserts records into a database table with
I have a PHP script that inserts data into a mysql database. The table
I have a function that builds an sql query and then inserts it into
I have a vbscript that inserts some strings into a database. Often, these strings
I want to save my canvas into MySQL database (BLOB cell). I have function
I have a function that does a long task and I want to update
I have a function that I want to execute on keyup for 2 different
How is it possible that this function below inserts two rows in my database?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.