MySQL query value Math.random gives the same number every time

I am trying to make a random tracking number and INSERT it into my database like this:

db.query('INSERT INTO orders SET ?', {tracking_number: Math.round(Math.random()* 100000000000 * Math.random()), order_date: date, ship_method: defaultShippingMethod, number_of_products: numberOfProducts}, (error, results) => {
    if(error) {
        console.log(error);
        return next();
    } else {
        return next();
    }
});

but if I were to try to use a variable like this:

let trackingNum = Math.round(Math.random()* 100000000000 * Math.random())

db.query('INSERT INTO orders SET ?', {tracking_number: trackingNum, order_date: date, ship_method: defaultShippingMethod, number_of_products: numberOfProducts}, (error, results) => {
        if(error) {
            console.log(error);
            return next();
        } else {
            return next();
        }
    });

the number inserted into my database is 2147483647 every time.

I have no idea what is going on

EDIT: The number was too big. It seems like 2147483647 is the max range for an INT in mysql or something along those lines. Either decrease the number the random val is multiplied by or store the value in a VARCHAR.



from Recent Questions - Stack Overflow https://ift.tt/3dJPs56
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation