2

I have a node.js script which takes in SMDR data from a phone system, then inserts it into a database. The code that I have right now inserts everything except for OperatorID (which it inserts [object Object] into that column). I cannot figure out as to why this is happening. Any ideas?

var net = require('net'),
   mysql = require('mysql'),
   PORT = 1752,
   HOST = '192.168.10.2',
   pool = mysql.createPool({
       host: 'localhost',
       port: 3306,
       user: 'root',
       password: 'root',
       database: 'mydb'
   });

function connectionListener(conn) {
    console.log('Listening for incoming calls...');
}

function logCall(phonenumber, operator) {
    pool.getConnection(function(err, connection) {
        if (!err) { // If no error exists 
            //var operID = '';
            var opquery = connection.query('SELECT OperatorID FROM tblOperators WHERE Phone_Login = ' + operator, function(err, rows) {
                if (err) {
                    console.error(err);
                }
                //operID = rows[0];
                //console.log(operID);
                console.log(rows[0]); //logs on console okay
                var query = connection.query('INSERT INTO incoming_calls(phone_number, OperatorID) VALUES("' + phonenumber + '", "' + rows[0] + '") ON DUPLICATE KEY UPDATE OperatorID=OperatorID, date_created=NOW()', function(err, rows) {//fails to insert rows[0]
                    if (err) {
                        console.error(err);
                    }
                    connection.release();
                });
            });
        } else {
            console.error(err);
        }
    });
}

function processdata(data) {
    var phonedata = data.toString().match(/([0-9]?)([0-9]{10})/),
    operdata = data.toString().match(/([*])([0-9]{4})/);
    if (phonedata !== null && operdata !== null) {
        var phonenumber = phonedata[2],
        oper = operdata[2];

       oper = oper.replace('*', '');
       //phonenumber = phonenumber.slice(0,3)+"-"+phonenumber.slice(3,6)+"-"+phonenumber.slice(6);
       logCall(phonenumber, oper);
    }
};

logCall('999-999-9999', '1203'); //where 1203 is valid
var conn = net.createConnection(PORT, HOST, connectionListener);
conn.on('data', processdata);
conn.setEncoding('utf8');

1 Answer 1

2

You need to specify

rows[0].OperatorID

in your INSERT query as you are trying to insert the whole row right now, which is an object.

var query = connection.query('INSERT INTO incoming_calls(phone_number, OperatorID) VALUES("' + phonenumber + '", "' + rows[0].OperatorID + '") ON DUPLICATE KEY UPDATE OperatorID=OperatorID, date_created=NOW()', function(err, rows) {//...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that was aggravating me, I just didn't catch that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.