Z
Z
zonf1k2020-03-05 13:56:37
MySQL
zonf1k, 2020-03-05 13:56:37

How to handle mysql connection error?

How to do this if the connection fails, no error occurred? (new to this)

const mysql = require("mysql2");

const pool = mysql.createConnection({
    host: "localhost",
    user: "root",
    database: "name",
    password: "password"
  });

 let reg = `INSERT INTO users(name) VALUES('name')`;
    
        pool.query(reg, function(err, results) {
        if(err) console.log(err);
    });

5e60daa385a95304889512.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Cheremkhin, 2020-03-05
@Che603000

pool.connect(function(err){
    if (err) {
      return console.error("Ошибка: " + err.message);
    }
    else{
      console.log("Подключение к серверу MySQL успешно установлено");
     // здесь можно делать запросы
    }
 });

V
Vladimir, 2020-03-05
@HistoryART

pool.connect(function(err){
    return err ? console.error("Ошибка: " + err.message) : console.log("Подключение к серверу MySQL успешно установлено");
    }
 });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question