Home > Enterprise >  How to connect db ID to a db Value in javascript?
How to connect db ID to a db Value in javascript?

Time:04-16

So I got a function that executes every X minutes/hours, when it executes it will select a random database array value, when it does, the selected will get 1. Selected stands for the amount of times the row has been selected.

id: 5 name: tom selected: 17
id: 8 name: tim selected: 12
id: 2 name: tombie selected: 5
id: 3 name: timbie selected: 4

This is my code from which I catch all of my database values

function setValue (value) {
  console.log("Array", value);
  result_arr = Object.values(JSON.parse(JSON.stringify(value)));
  return result_arr;
  
}
console.log("arr", result_arr);
db.connect(function(err) {
    db.query("SELECT * FROM developers", (err, result, fields) => {
        if (err) {
        console.log(err);
        } else {
        // selected = all.map(item => item.selected);
        console.log("result", result);
        setValue(result);
        console.log("value", setValue(result));
        removeMax()
        
        }
    })

Lets say if we have Selected: 5, 4, 12 and 17. 17 is the highest selected count, to avoid it gaining more selected points, I ensure the highest numbers is removed from the array. Once the highest number is removed, I shuffle the numbers and it will select a random one.

function removeMax() {
    const max = Math.max(result_arr.map(item => item.selected));
    const newArr = result_arr.map(item => item.selected).filter(number => number !== max);
    var shufflearray = newArr[Math.floor(Math.random()*newArr.length)];
    return shufflearray;
    
}

Lets say from the shuffle I get 5. I now have to add 1 to this, so 5 must become 6.

In function hi() I have an If statement, everyday at 8 am this code has to be executed. So as I said, I have to update the selected value.

Here comes the question: Usually I would do UPDATE developers SET selected = 6 WHERE id = 2. but since I have multiple rows this wont work. Today It might be Timbie, tommorow It can be Tim. How do I link the selected values with their IDs?

function hi() {
    window.setInterval(function(){
        var date = new Date();
        if (date.getHours() === 8 && date.getMinutes() === 0) {
            if (test) {
                db.query( `UPDATE developers SET selected = ? WHERE id = ${item.id} `,[selected, id],
                (err, result) => {
                    if (err) {
                        console.log(err);
                    } else {
                        res.send(result);
                    }
                });
            }
  





        }
        else {
            const abort = "abort"
        }
    }, 60000);
}

CodePudding user response:

In the function removeMax(), the data is reduced to become just a list of the selected values. You would need to keep each item, not only the number of times it is selected.

Original code:

function removeMax() {
    const max = Math.max(result_arr.map(item => item.selected));
    const newArr = result_arr.map(item => item.selected).filter(number => number !== max);
    var shufflearray = newArr[Math.floor(Math.random()*newArr.length)];
    return shufflearray;
    
}

Original result:

var result_arr = [{'id': 1, 'selected': 10}, {'id': 2, 'selected': 5}, {'id': 3, 'selected': 15}, {'id': 4, 'selected': 7}]

newArr = result_arr.map(item => item.selected)
resulting newArr: [10, 5, 15, 7]

After that, you are removing the largest value. Notice how all the id's are gone?

If you instead do:

newArr = result_arr.filter(item => item.selected !== max)

resulting newArr: [{id: 1, selected: 10}, {id: 2, selected: 5}, {id: 4, selected: 7}]

Now that you have an array of objects, you can rearrange them as before, and afterwards access the # of selected with item.selected, and each id with item.id.

  • Related