I have my GET endpoint which is just a simple SELECT * FROM tableName and returns all the rows in the table but it keeps hanging. When I hit it in POSTMAN locally it just keep spinning with
Sending request... and the program never errors. The get endpoint is hanging after connection.query(...) and the fourth console.log("HIT) is not printed
router.get('/allRows', async (req, res) => { console.log("HIT") if (req.query.tableName) { console.log("HIT") connection.connect((err) => { if (err) console.log(err); console.log("HIT") connection.query(`SELECT * FROM ${req.query.tableName};`), (err, result, fields) => { console.log("HIT") if (err) console.log(err); console.log(result) res.status(200).json({ message: `Successfully got all data from ${req.query.tableName}`, result: result }); }; connection.end((err) => { if (err) console.error("Error when closing connection", err) }); }) } else { res.status(400).json({ message: `Please provide table name`, }); console.log('Missing table name'); } }); I have 3 "HIT" logs at the moment, any ideas?
I noticed that if I cancel the endless request in postman and I hit the endpoint again I get a PROTOCOL_ENQUEUE_AFTER_QUIT error
EDIT
I changed my code to this:
router.get('/allRows', async (req, res) => { console.log("HIT") // also another guard for table name inside an array is needed if (req.query.tableName) { console.log("HIT") connection.connect(); connection.query(`SELECT * FROM ${req.query.tableName}`), (err, rows, fields) => { console.log("HIT", rows) if (err) console.log(err); res.status(200).json({ message: `Successfully got all data from ${req.query.tableName} LIMIT 1`, result: rows }); console.log("result") }; } else { res.status(400).json({ message: `Please provide table name`, }); console.log('Missing table name'); } }); Still get the same issue and only 2 console hits... I have ran the debugger and when it gets to connection.query it just skips over it and doesn't go into the arrow function.
debugger after connection.query
https://stackoverflow.com/questions/65877954/node-mysql-hanging January 25, 2021 at 09:47AM
没有评论:
发表评论