我有一个程序可以将SMDR数据插入到数据库中。
这是我的代码:
var net = require('net'),
mysql = require('mysql'),
PORT = 1752,
HOST = '192.168.10.2',
pool = mysql.createPool({
host: 'localhost',
port: 3307,
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 opquery = connection.query('SELECT OperatorID FROM tblOperators WHERE Phone_Login = ' + operator, function(err, rows) {
if (err) {
console.error(err);
connection.release();
return;
}
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) {
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');
var conn = net.createConnection(PORT, HOST, connectionListener);
conn.on('data', processdata);
conn.setEncoding('utf8');
这是我得到的错误,当表中显然存在OperatorID时
c:\xampp\htdocs>node listener
Listening for incoming calls...
c:\xampp\htdocs\node_modules\mysql\lib\protocol\Parser.js:82
throw err;
^
TypeError: Cannot read property 'OperatorID' of undefined
at Query._callback (c:\xampp\htdocs\listener.js:27:48)
at Query.Sequence.end (c:\xampp\htdocs\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Query._handleFinalResultPacket (c:\xampp\htdocs\node_modules\mysql\lib\protocol\sequences\Query.js:143:8)
at Query.EofPacket (c:\xampp\htdocs\node_modules\mysql\lib\protocol\sequences\Query.js:127:8)
at Protocol._parsePacket (c:\xampp\htdocs\node_modules\mysql\lib\protocol\Protocol.js:271:23)
at Parser.write (c:\xampp\htdocs\node_modules\mysql\lib\protocol\Parser.js:77:12)
at Protocol.write (c:\xampp\htdocs\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (c:\xampp\htdocs\node_modules\mysql\lib\Connection.js:82:28)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:764:14)
有没有人对于为什么会发生这种情况有任何想法,我有一个使用它的生产数据库,具有相同的确切信息并且有效吗?
答案 0 :(得分:1)
首先检查您的查询结果是否为空,如果它是空白的,那么您将获得该错误。
var opquery = connection.query('SELECT OperatorID FROM tblOperators WHERE Phone_Login = ' + operator, function(err, rows) {
if (err) {
console.error(err);
connection.release();
return;
}
else
{
if(rows!="")
{
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) {
if (err) {
console.error(err);
}
connection.release();
});
}
else
{
console.log('can not get data from tabel');
}
}