Home > Mobile >  Connected to the database but through error when I try to insert into database table
Connected to the database but through error when I try to insert into database table

Time:01-16

I am not able to insert data to the database. There is no issues connected to the database but when I try to insert into table, It through an error.

Error: Cannot enqueue Query after fatal error.

How can I solve this problem? Anyone facing same problem?

const { redirect } = require("express/lib/response");
const mysql = require('mysql')
let con = mysql.createConnection({
    host: process.env.db_host,
    database: process.env.db_database,
    username: process.env.db_username,
    password: process.env.db_password,
    port: process.env.db_port
});
con.connect(err=>{
    {err===true? console.log(err): console.log("DB connected Successfully!!");;}
})
// Home Page
exports.index = (req,res)=>{
    res.render('../views/index', {btnName: 'Save' })    
    console.log("You are in home page!");
}
// // Add User 
// exports.addUser = (req,res)=>{
//     res.render('adduser')
// }
exports.adduserpost = (req,res)=>{
const {fname, mname, lname, email, phone} = req.body; //store value from adduser
// console.log({fname, mname, lname, email, phone});
let sql = 'INSERT INTO user_info (fname, mname, lname, email, phone) VALUES (?,?,?,?,?)';
con.query(sql,[fname, mname, lname, email, phone],
        (err,result)=>{
            if(!err) console.log(result);
            else console.log( err);
})

Through Error like this.

Server connected successfully!!
DB connected Successfully!!
Error: Cannot enqueue Query after fatal error.
    at Protocol._validateEnqueue (F:\Project\User_Information_System_v2\node_modules\mysql\lib\protocol\Protocol.js:212:16)
    at Protocol._enqueue (F:\Project\User_Information_System_v2\node_modules\mysql\lib\protocol\Protocol.js:138:13)
    at Connection.query (F:\Project\User_Information_System_v2\node_modules\mysql\lib\Connection.js:198:25)
    at exports.adduserpost (F:\Project\User_Information_System_v2\server\controller\userController.js:53:5)
    at Layer.handle [as handle_request] (F:\Project\User_Information_System_v2\node_modules\express\lib\router\layer.js:95:5)
    at next (F:\Project\User_Information_System_v2\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (F:\Project\User_Information_System_v2\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (F:\Project\User_Information_System_v2\node_modules\express\lib\router\layer.js:95:5)
    at F:\Project\User_Information_System_v2\node_modules\express\lib\router\index.js:281:22  
    at Function.process_params (F:\Project\User_Information_System_v2\node_modules\express\lib\router\index.js:341:12) {
  code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
  fatal: false
}

CodePudding user response:

You should change your connect error checking condition.
By using err===true the message DB connected Successfully!! will be displayed also if the err is present as an object.
Try to change your code like this and see if it logs any error messages:

con.connect(err=>{
    if(err) {
        console.log(err);
        process.exit(1);
    } 
    console.log("DB connected Successfully!!")
})
  •  Tags:  
  • Related