Heroku:Javascript / MySQL项目在请求时崩溃

时间:2020-06-17 18:43:32

标签: javascript mysql node.js heroku npm

我有一个用JavaScript编写的Heroku API,它可以从MySQL数据库读取数据。以前可以使用,但是现在每次我向API发送请求时,都会出现此错误:

2020-06-17T18:37:13.493711+00:00 app[web.1]: > my_api@0.0.0 start /app
2020-06-17T18:37:13.493712+00:00 app[web.1]: > node ./bin/www
2020-06-17T18:37:13.493712+00:00 app[web.1]: 
2020-06-17T18:37:14.977800+00:00 heroku[web.1]: State changed from starting to up
2020-06-17T18:38:40.255982+00:00 app[web.1]: /app/node_modules/mysql2/lib/connection.js:149
2020-06-17T18:38:40.256022+00:00 app[web.1]:     err.fatal = true;
2020-06-17T18:38:40.256023+00:00 app[web.1]:               ^
2020-06-17T18:38:40.256023+00:00 app[web.1]: 
2020-06-17T18:38:40.256026+00:00 app[web.1]: TypeError: Cannot create property 'fatal' on string 'SOCKS: Authentication failed'
2020-06-17T18:38:40.256027+00:00 app[web.1]:     at Connection._handleFatalError (/app/node_modules/mysql2/lib/connection.js:149:15)
2020-06-17T18:38:40.256027+00:00 app[web.1]:     at Connection._handleNetworkError (/app/node_modules/mysql2/lib/connection.js:169:10)
2020-06-17T18:38:40.256027+00:00 app[web.1]:     at SocksConnection.emit (events.js:315:20)
2020-06-17T18:38:40.256029+00:00 app[web.1]:     at /app/node_modules/socksjs/socks.js:146:18
2020-06-17T18:38:40.256029+00:00 app[web.1]:     at Socket.dataReady (/app/node_modules/socksjs/socks.js:93:13)
2020-06-17T18:38:40.256029+00:00 app[web.1]:     at Socket.emit (events.js:315:20)
2020-06-17T18:38:40.256030+00:00 app[web.1]:     at emitReadable_ (_stream_readable.js:556:12)
2020-06-17T18:38:40.256030+00:00 app[web.1]:     at processTicksAndRejections (internal/process/task_queues.js:83:21)
2020-06-17T18:38:40.263200+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-17T18:38:40.263391+00:00 app[web.1]: npm ERR! errno 1
2020-06-17T18:38:40.264251+00:00 app[web.1]: npm ERR! my_api@0.0.0 start: `node ./bin/www`
2020-06-17T18:38:40.264354+00:00 app[web.1]: npm ERR! Exit status 1
2020-06-17T18:38:40.264471+00:00 app[web.1]: npm ERR! 

有问题的代码如下:

function new_db_connection() {
    var sockConn = new SocksConnection(remote_options, sock_options);
    var dbConnection = mysql.createConnection({
    user: 'myusername',
    database: 'mydatabase',
    password: 'mypassword',
    stream: sockConn
});
    return [sockConn, dbConnection];
};


router.get('/myrequest', function(req, res, next) {
    new_conn = new_db_connection();
    dbConnection = new_conn[1];
    sockConn = new_conn[0];
    dbConnection.query('SELECT * from mytable', function(error, results, fields) {
        if (error) {
            res.send(JSON.stringify({
                "status": 500,
                "error": error,
                "response": null
            }));
            //If there is error, we send the error in the error section with 500 status
        } else {
            res.send(JSON.stringify({
                "status": 200,
                "error": null,
                "response": results
            }));
            //If there is no error, all is good and response is 200OK.
        }
        sockConn.dispose();
    });
    dbConnection.end()
});

我在这里看到一个类似的问题,解决方案是关闭连接,但是我要做在每次请求后关闭连接,所以我不知道问题出在哪里。

感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

尝试更改此内容

        sockConn.dispose();
    });
    dbConnection.end()
});

在此

        sockConn.dispose();
        dbConnection.end()
    });
});

编辑:更好地阅读错误,可能是其他地方存在问题。

看看字符串'SOCKS: Authentication failed',是您还是SocksConnection远程端点更改了身份验证中的某些内容?

希望这会有所帮助。

答案 1 :(得分:1)

您应该查看@daniele Rici提供的答案,您需要正确结束连接;但是,解决此问题不会解决您的第一个问题。

问题与您的代码无关。 您的问题与连接配置有关。

字符串“ SOCKS:致命”上的“致命”

意味着身份验证出错。

var sockConn = new SocksConnection(remote_options, sock_options);

假设您正在使用https://openbase.io/js/socksjs的npm软件包socksjs

有文献证明,初始化sockjs连接的正确方法是执行以下操作:

var sock = new SocksConnection(remote_options, sock_options); 
var sock = SocksConnection.connect(remote_options, sock_options, connect_handler);