sending message using results from database sql node.js


I have this function. Apologies in advance for the terrible variable names.

    //running cronJob every 45 seconds on the minute
var getTexts = new cronJob( '45 * * * * *', function(){
    let weekday = ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'][new Date().getDay()]

    console.log(weekday)
    var viewConformationEmails = "select " + weekday + " from clients";
    ibmdb.open(ibmdbconn, function(err, conn) {
        if (err) return console.log(err);
        conn.query(viewConformationEmails, function(err, rows) {
            if (err) {
                console.log(err);
                var today = new Date();
                var time = today.getMinutes() + " " + today.getHours() + " * " + "* " + "*"
                var errorJob = new cronJob( time, function(){
                    client.messages.create( { to: "0000000000", from:'12055578708', body:'Error with your code: ' + err}, function( err, data ) {});
                }, null, true);
            }
            

            var today = new Date();
            var time = today.getMinutes() + " " + today.getHours() + " * " + "* " + "*"

            //note: when you're testing, monday = day 1
            for (var i = 0; i < rows.length; i++) {
                var sessionString = rows[i][weekday];
                var finalSessionString = sessionString.split('|');
                var finalSessionString0 = finalSessionString[0]
                var finalSessionStringPhoneNumber = finalSessionString[1]
                var finalSessionStringName = finalSessionString[2]
                var finalSessionStringTrainerName = finalSessionString[3]
                var textJob = new cronJob( finalSessionString0, function(){
                    client.messages.create( { to: finalSessionStringPhoneNumber, from:'12055578708', body:'hello ' + finalSessionStringName + ', Your training session just finished with ' +  finalSessionStringTrainerName}, function( err, data ) {
                        if (err) {
                            console.log("err: " + err)
                        }
                    });
                }, null, true);
               
            }
            conn.close(function() {
                // console.log("closed the function /login");
            });
        });
    });
}, null, true)

What this function does is every 45 seconds it runs a cronjob, querying the database table, and selecting from a column, specifically the column that is named after today’s current day (ex. monday).

what is then does is returns all the rows in said column, returning the rows in a structure like so (NOTE* I HAVE 3 ROWS IN THERE RIGHT NOW, ALL 3 ARE BELOW):

['45 21 * * 1|2897791084|lola a|[email protected]']

above i am only showing one record, to show the structure. but there’s 3, and they all get split. then what i do is split them with the .split you see above. as you can see below, it returns this, all split up into an array, all 3 results.

[
      '45 21 * * 1',
      '2897791084',
      'lola A',
      '[email protected]'
    ]
    [
      '02 22 * * 1',
      '0123456789',
      'D-DAWG A',
      '[email protected]'
    ]
    [
      '46 21 * * 1',
      '987654321',
      'Yols A',
      '[email protected]'
    ]

So then I grab each record from the array, the one necessary, like the phonenumber, using finalSessionString[1], and so on for each one. then is should send a text message, if the time is the current one.

The problem is, it is sending to the wrong record, with the current time. So it is sending record 2 using the record 2 time, but taking the name, email, and phone number of the LAST record. It is sending to the phone number of the last record, using the time of the record in which its time matches the current time. WEIRD, i know. not sure why, can’t figure it out at all. Any ideas?

Source: JavaSript – Stack Overflow

November 2, 2021
Category : News
Tags: cron | javascript | node.js | SQL | twilio

Leave a Reply

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

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.