mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.22k stars 2.53k forks source link

pattern.replace is not a function #2510

Closed rvwhitney closed 2 years ago

rvwhitney commented 2 years ago

all of a sudden I am getting the following error when deleting a file:

TypeError: pattern.replace is not a function at patternRegExp (/home/ubuntu/Public/node_modules/mysql/lib/PoolCluster.js:276:6) at PoolCluster._findNodeIds (/home/ubuntu/Public/node_modules/mysql/lib/PoolCluster.js:176:22) at PoolNamespace._getClusterNode (/home/ubuntu/Public/node_modules/mysql/lib/PoolNamespace.js:118:36) at PoolNamespace.getConnection (/home/ubuntu/Public/node_modules/mysql/lib/PoolNamespace.js:21:26) at PoolCluster.getConnection (/home/ubuntu/Public/node_modules/mysql/lib/PoolCluster.js:145:13)

Obviously pattern is either not defined or not a string, but I can't tell what is being passed.

dougwilson commented 2 years ago

When your code calls .getConnection, is it passing in a pattern? If not it would be the set default pattern defined for your cluster. We could throw a type error earlier, though that won't fix your issue, just make the error state invalid pattern.

Is it poasible you can provide the code to reproduce the issue? I can help dig in to the cause.

rvwhitney commented 2 years ago

Thanks for the quick response Doug!

Here is the relevant code:

function getConn(query_name, conns, params, callback) {
log(__line__, query_name, conns, params);
    var pool = poolCluster;
    if (!pool) {
        exec('/home/ubuntu/sh/nodestart.sh');
        log(__line__, 'no pool');
        if(typeof callback === 'function'){
            callback(new Error('Missing database connection.'));
        } else {
            log(__line__,  callback);
        }
    }
    log(__line__, now(),'\nDATA UTIL QTYPE '+query_name);
    poolCluster.getConnection(datautil['queries'][query_name].conn, function (err, connection) {
        //~ connection.release();
        if (err) {
            log(__line__, 'Connection refused...\n' + err);
            exec('/home/ubuntu/sh/nodestart.sh', puts);
            return callback(err);
        } else {
            return callback(null, connection);
        }
    });
}

connect : function (callback) {
    poolCluster = mysql.createPoolCluster();
    poolCluster.add('STRG',     sys_conns.STRG);
    poolCluster.add('META',     sys_conns.META);
    poolCluster.add('SHARED',   sys_conns.SHARED);
    poolCluster.add('SEC',      sys_conns.SEC);
    poolCluster.add('SEC_UTIL', sys_conns.SEC_UTIL);
    for(var node in poolCluster._nodes) {
        poolCluster._nodes[node].pool.on('acquire', function (connection) {
            openConns[connection.threadId] = 'acquired';
            //~ log(__line__, 'openConns: ', openConns);
        });
        //~ poolCluster._nodes[node].pool.on('connection', function (connection) {
            //~ log(__line__, 'connection', connection.threadId, 'created');
        //~ });
        poolCluster._nodes[node].pool.on('enqueue', function () {
            log(__line__, 'request enqueued');
        });
        poolCluster._nodes[node].pool.on('release', function (connection) {
            delete openConns[connection.threadId];
            log(__line__, now() + ' - openConns: ', openConns);
        });
    }
    return callback();
},

socket.on('remove-file', function(data){
    getSession(session.key, function(currSession){
        db.rows('removeFile', currSession, [data, currSession.user, data, currSession.user], function(err, rows){
            if(!data){
                socket.emit('err', 'Error is: Nothing selected' );
            } else if (err) {
                socket.emit('err', 'Error is: ' + err );
            } else {
                socket.emit('remove-file');
                socket.emit('success' , 'File(s) Successfully Removed.' );
            }
        });
    });
});

rows : function(query, conns, params, callback) {
    getConn (query, conns, params, function(err, connection) {
        if (err){
            callback('No db connection');
        }
        //~ return log(__line__, 'Success!');
        var sql = datautil.queries[query].sql;
        sql = mysql.format(sql, params);
        log(__line__, 'THIS IS SQL ROWS: ',sql);
        if(query === 'sharedMultiple'){
            sql = sql.replace('#',params[1]);
        }

        log(__line__, 'THIS IS SQL IN ROWS: ',query,sql);

        connection.query(sql, function(err, row){
            if(err){
                callback(err);
            }
            //~ log(__line__, err, row);
            connection.release();
            if(typeof callback === 'function'){
                return callback(err, row);
            }

        });
    });
},
rvwhitney commented 2 years ago

The related query:

removeFile : {
    conn : ['META'],
    sql :   "DELETE FROM STORAGE.TBL_ATTACHMENT_FILE WHERE ID IN (SELECT FILEID FROM META.TBL_ATTACHMENT WHERE ID IN (?) AND USERENTERED = ?); " +
            "DELETE FROM META.TBL_ATTACHMENT WHERE ID IN (?) AND USERENTERED = ?;"
},

removeFile is the queryName in datautil['queries'][query_name].conn conn is META above

rvwhitney commented 2 years ago

figured it out - META was an array, not a string - pretty simple error - sorry for troubling you!