A
A
Andrey2022-04-07 22:19:23
JavaScript
Andrey, 2022-04-07 22:19:23

Connection pool and how it works?

Hello to all!
There is a code:

const mysql = require("mysql2");
const pool = mysql.createPool({
    host: "localhost",
    user: "use",
    database: "dadb",
    password: "pass"
   }).promise();
const sql = 'SELECT secret FROM validation_table WHERE name=?;'
const name = 'flowerr'
pool.query(sql,name)
.then(result =>{
    let res = result[0]
    console.log('result = ',res[0]);
    pool.end();
})
.then(() =>{
  console.log("end"); 
})
.catch(function(err) {
  console.log(err.message);
})

result in console:
result =  undefined
end

How to break the script on the first ".then" if the received value = "undefined"?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
Quasimord Sidorovich, 2022-04-09
@chelitsy

.then(result =>{
    let res = result[0]
    console.log('result = ',res[0]);
    pool.end();
if( result === undefined ) throw new Error();
})
.then(() =>{
  console.log("end"); 
})
.catch(function(err) {
  console.log(err.message);
})

if you throw your error, then the next then will be skipped, and the error will be caught in catch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question