前提
node.js、heroku、テーブルプラスの組み合わせで開発をしており、完成したかと思いテーブルプラスを閉じたら以下のようなエラーが起きてしまいました。
実現したいこと
テーブルプラスを閉じてもエラーが出ないようにする。
発生している問題・エラーメッセージ
2022-08-08T06:38:02.676921+00:00 app[web.1]: node:events:505 2022-08-08T06:38:02.676942+00:00 app[web.1]: throw er; // Unhandled 'error' event 2022-08-08T06:38:02.676943+00:00 app[web.1]: ^ 2022-08-08T06:38:02.676943+00:00 app[web.1]: 2022-08-08T06:38:02.676943+00:00 app[web.1]: Error: Connection lost: The server closed the connection. 2022-08-08T06:38:02.676944+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:112:13) 2022-08-08T06:38:02.676944+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:94:28) 2022-08-08T06:38:02.676945+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10) 2022-08-08T06:38:02.676945+00:00 app[web.1]: at Socket.emit (node:events:539:35) 2022-08-08T06:38:02.676945+00:00 app[web.1]: at endReadableNT (node:internal/streams/readable:1345:12) 2022-08-08T06:38:02.676945+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21) 2022-08-08T06:38:02.676946+00:00 app[web.1]: Emitted 'error' event on Connection instance at: 2022-08-08T06:38:02.676948+00:00 app[web.1]: at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:423:8) 2022-08-08T06:38:02.676948+00:00 app[web.1]: at Protocol.emit (node:events:527:28) 2022-08-08T06:38:02.676948+00:00 app[web.1]: at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10) 2022-08-08T06:38:02.676949+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:116:8) 2022-08-08T06:38:02.676949+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:94:28) 2022-08-08T06:38:02.676949+00:00 app[web.1]: [... lines matching original stack trace ...] 2022-08-08T06:38:02.676949+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21) { 2022-08-08T06:38:02.676950+00:00 app[web.1]: fatal: true, 2022-08-08T06:38:02.676950+00:00 app[web.1]: code: 'PROTOCOL_CONNECTION_LOST' 2022-08-08T06:38:02.676950+00:00 app[web.1]: } 2022-08-08T06:38:02.825924+00:00 heroku[web.1]: Process exited with status 1 2022-08-08T06:38:03.093120+00:00 heroku[web.1]: State changed from up to crashed 2022-08-08T06:38:03.097550+00:00 heroku[web.1]: State changed from crashed to starting 2022-08-08T06:38:04.816111+00:00 heroku[web.1]: Starting process with command `npm start` 2022-08-08T06:38:06.622322+00:00 app[web.1]: 2022-08-08T06:38:06.622337+00:00 app[web.1]: > list-app@1.0.0 start 2022-08-08T06:38:06.622337+00:00 app[web.1]: > node app.js 2022-08-08T06:38:06.622338+00:00 app[web.1]: 2022-08-08T06:38:07.192969+00:00 heroku[web.1]: State changed from starting to up 2022-08-08T06:43:52.000000+00:00 app[api]: Build started by user akiress55555@gmail.com 2022-08-08T06:44:08.654774+00:00 app[api]: Deploy 593c6cb6 by user akiress55555@gmail.com 2022-08-08T06:44:08.654774+00:00 app[api]: Release v34 created by user akiress55555@gmail.com 2022-08-08T06:44:09.000000+00:00 app[api]: Build succeeded 2022-08-08T06:44:09.110583+00:00 heroku[web.1]: Restarting 2022-08-08T06:44:09.113186+00:00 heroku[web.1]: State changed from up to starting 2022-08-08T06:44:10.644399+00:00 heroku[web.1]: Stopping all processes with SIGTERM 2022-08-08T06:44:10.820846+00:00 heroku[web.1]: Process exited with status 143 2022-08-08T06:44:10.832446+00:00 heroku[web.1]: Starting process with command `npm start` 2022-08-08T06:44:12.602277+00:00 app[web.1]: 2022-08-08T06:44:12.602304+00:00 app[web.1]: > list-app@1.0.0 start 2022-08-08T06:44:12.602304+00:00 app[web.1]: > node app.js 2022-08-08T06:44:12.602305+00:00 app[web.1]: 2022-08-08T06:44:12.762807+00:00 app[web.1]: /app/app.js:75 2022-08-08T06:44:12.762823+00:00 app[web.1]: var pool = mysql.createPool(db_config); 2022-08-08T06:44:12.762826+00:00 app[web.1]: ^ 2022-08-08T06:44:12.762826+00:00 app[web.1]: 2022-08-08T06:44:12.762826+00:00 app[web.1]: ReferenceError: db_config is not defined 2022-08-08T06:44:12.762827+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:75:41) 2022-08-08T06:44:12.762829+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1105:14) 2022-08-08T06:44:12.762830+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) 2022-08-08T06:44:12.762830+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32) 2022-08-08T06:44:12.762830+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12) 2022-08-08T06:44:12.762834+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) 2022-08-08T06:44:12.762835+00:00 app[web.1]: at node:internal/main/run_main_module:17:47 2022-08-08T06:44:12.902261+00:00 heroku[web.1]: Process exited with status 1 2022-08-08T06:44:12.968627+00:00 heroku[web.1]: State changed from starting to crashed 2022-08-08T06:44:22.505661+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=kaimemo1993.herokuapp.com request_id=293f364a-26ef-44c3-9eeb-3d4250f8826b fwd="133.32.129.34" dyno= connect= service= status=503 bytes= protocol=https 2022-08-08T06:44:51.660691+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=kaimemo1993.herokuapp.com request_id=f29027c4-e4f2-4962-9f80-9fa778a476e7 fwd="133.32.129.34" dyno= connect= service= status=503 bytes= protocol=https 2022-08-08T06:46:13.956066+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=kaimemo1993.herokuapp.com request_id=795da724-b0a2-4513-a6fc-bc4f0f035cc1 fwd="133.32.129.34" dyno= connect= service= status=503 bytes= protocol=https 2022-08-08T06:53:01.125367+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=kaimemo1993.herokuapp.com request_id=ae5643af-e065-45c4-8556-d63f5e7a68b8 fwd="133.32.129.34" dyno= connect= service= status=503 bytes= protocol=https
該当のソースコード
const express = require('express'); const mysql = require('mysql'); const app = express(); app.use(express.static('public')); app.use(express.urlencoded({extended: false})); const connection = mysql.createConnection({ host: 'us-cdbr-east-06.cleardb.net', user: 'b30232f422ebcf', password: '4d1d89e0', database: 'heroku_00afb7330dc1246' }); app.get('/', (req, res) => { res.render('top.ejs'); }); app.get('/index', (req, res) => { connection.query( 'SELECT * FROM items', (error, results) => { res.render('index.ejs', {items: results}); } ); }); app.get('/new', (req, res) => { res.render('new.ejs'); }); app.post('/create', (req, res) => { connection.query( 'INSERT INTO items (name) VALUES (?)', [req.body.itemName], (error, results) => { res.redirect('/index'); } ); }); app.post('/delete/:id', (req, res) => { connection.query( 'DELETE FROM items WHERE id = ?', [req.params.id], (error,results) => { res.redirect('/index'); } ); }); app.get('/edit/:id', (req, res) => { connection.query( 'SELECT * FROM items WHERE id = ?', [req.params.id], (error, results) => { res.render('edit.ejs',{item:results[0]}); } ); }); app.post('/update/:id', (req, res) => { connection.query( 'UPDATE items SET name = ? WHERE id = ?', [req.body.itemName , req.params.id], (error, results) => { res.redirect('/index'); } ); }); var pool = mysql.createPool(db_config); pool.getConnection(function(err, connection){ connection.query('SELECT * FROM items WHERE id = ?', function(err, rows, fields){ connection.release(); }); }); app.listen(process.env.PORT || 3000);
試したこと
https://ninna2.hatenablog.com/entry/2017/02/22/node-mysql%E3%81%A7%E6%8E%A5%E7%B6%9A%E3%81%8C%E5%88%87%E3%82%8C%E3%82%8B%E7%82%B9%E3%82%92%E6%94%B9%E5%96%84
こちらのサイトを参考に試してみたのですがエラーが起きてしまいます。

0 コメント