Article From:

I ran into a problem in the node-schedule module. I circulate an array order in a timed task, with an array length of only 2, which would create a piece of data in the data table once a cycle. In reason, it should only create two data, but this array is based on the timing task.Four pieces of data have been created two times around. If the for loop is placed outside the timing task scheduleJob, the array will only be circulated and solved.

The code is as follows:

schedule.scheduleJob('30 50 15 * * *',function(){
    console.log('The timing task ');For (let i=0; i< order.length; i++) {Console.log ("for cycle");Console.log ('I: ', I ";Const finalPrice = order[i].finalPrice;Const earning = countEarning (finalPriCE, selfRate);Const divided = countEarning (finalPrice, uplineRate);Console.log ("earnIng: ", earning);Console.log ("divided:", divided);Const orderearnMsg = {{Uid: order[i].uid,Oid: order[i].orderId,Price: order[i].finalPrice,Earning: earning,Divided: divided,Year: year,Month: monTh,Day: day,Time: time}Orderearn.create (orderearnMsg).ThEn (function (orderearnRes) {)Console.log ('create success');})}})

Answer 0:

I didn’t understand this timed module. I used setinterval to test the timing tasks I wrote, and call every 15 seconds. I thought that no matter how high the frequency of the call, I would execute only one second, and I didn’t expect to perform many times in a second (hand over my face).Therefore, the for cycle is executed many times.

Answer 1:

It should not be the problem of the timer, but the problem of your for loop. You are an asynchronous operation in the for loop. It may be a problem, and you can’t get the result you want. Rewrite as follows:

    var o=order[i];

Leave a Reply

Your email address will not be published. Required fields are marked *