How can I send a json data and run a index file at the same time in res.resnder? is it even possible?

Viewed 40

I am trying to render the index page and display the data that I am getting from a table.

BUT am getting an error

THE ERROR IS "C:\Users\acer\Desktop\nodejsyt\index.js:23 if (err) throw err; ^

Error: Cannot enqueue Handshake after already enqueuing a Handshake. at Protocol._validateEnqueue (C:\Users\acer\Desktop\nodejsyt\node_modules\mysql\lib\protocol\Protocol.js:221:16) at Protocol._enqueue (C:\Users\acer\Desktop\nodejsyt\node_modules\mysql\lib\protocol\Protocol.js:138:13) at Protocol.handshake (C:\Users\acer\Desktop\nodejsyt\node_modules\mysql\lib\protocol\Protocol.js:51:23) at Connection.connect (C:\Users\acer\Desktop\nodejsyt\node_modules\mysql\lib\Connection.js:116:18) at getallparent (C:\Users\acer\Desktop\nodejsyt\index.js:22:20) at C:\Users\acer\Desktop\nodejsyt\index.js:19:5 at Layer.handle [as handle_request] (C:\Users\acer\Desktop\nodejsyt\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\acer\Desktop\nodejsyt\node_modules\express\lib\router\route.js:144:13) at Route.dispatch (C:\Users\acer\Desktop\nodejsyt\node_modules\express\lib\router\route.js:114:3) at Layer.handle [as handle_request] (C:\Users\acer\Desktop\nodejsyt\node_modules\express\lib\router\layer.js:95:5) { code: 'PROTOCOL_ENQUEUE_HANDSHAKE_TWICE', fatal: false }

Node.js v18.5.0 [nodemon] app crashed - waiting for file changes before starting..."

var hbs = require('hbs');
var path = require('path');
var express = require('express');
var app = express();

app.set('view engine', 'hbs');

//CONNECTION CONFIGURATION
var con = mysql.createConnection({
 host: "localhost",
 user: "root",
 password: "",
 database: "mydb"
});

app.get('/', (req,res)=> {

 getallparent();

 function getallparent(){
     return con.connect(function(err) {
         if (err) throw err;
         con.query("SELECT * FROM parents", function (err, result) {
         if (err) throw err;
             res.render('index' , result);
         });
     });
   };
   
});

app.listen(3000);```
2 Answers

You have to remove:
con.connect(function(err) {
When initiating connection queries they automatically connect to the db on the background, so in your case you are trying to create 2 connections simultaneously , What should work for you is:

   return connection.query("SELECT * FROM parents", function (error, results) {
      if (error) throw error;
      // connected!
      res.render('index' , result);
    });

So I finally figured out my problem I was doing it all correctly, but when I was trying to display the sent data on the index page I was displaying it wrongly ( I was using HBS "HANDLEBARS" as my view template and this is the right way {{firstname}} to display the sent data on the index page. )

var express = require('express');
var app = express();
var mysql = require('mysql');
const router = express.Router();

app.set('view engine', 'hbs');

//CONNECTION CONFIGURATION
var con = mysql.createConnection({
 host: "localhost",
 user: "root",
 password: "",
 database: "node"
});

// ALL PARENTS
app.get('/index', (req,res)=> {
    return con.query("SELECT * FROM parents", function (error, results) {
        if (error) throw error;
        // connected!
        res.render('index',results);
      });
});

app.listen(3000);
Related