javascript Node.js mysql 错误无法读取未定义的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27611042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:42:13  来源:igfitidea点击:

Node.js mysql error cannot read property of undefined?

javascriptmysqlnode.js

提问by Juan

I have a program that inserts SMDR data into a database as it comes in.

我有一个程序可以将 SMDR 数据插入到数据库中。

Here is my code:

这是我的代码:

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');

And here is the error that I get, when OperatorID clearly does exist within the table:

这是我得到的错误,当 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)

Does anyone have any ideas as to why this would happen, I have a production database that uses this, has the same exact information and it works?

有没有人知道为什么会发生这种情况,我有一个使用它的生产数据库,具有相同的确切信息并且可以正常工作?

回答by Alex

first check your query result is blank or not if it blank than you will get that error ex.

首先检查您的查询结果是否为空,如果它为空,您将收到该错误,例如。

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');
              }
            }