Nodejs connection.connect is not a function - SQL query results is undefined

Viewed 177

I was following a tutorial trying to connect Node.js with Mysql database . I'm using xampp and phpmyadmin

This is the output on the terminal :

connection.connect is not a function

listening on port 3000...

undefined

(I've alread run npm install mysql2)

Database name : proglang

Database table :

enter image description here

and here is the code:

const express = require('express');
const mysql = require('mysql2/promise');
const app = express();


const query= async (sql)=>{
try{
    const connection =  mysql.createConnection(
{     
    host: '127.0.0.1',
    user : 'root',
    password:'',
    database : 'proglang' ,
    port: 3306
});
  connection.connect((err)=>{
      if(err){
          console.log(err);
      }
      else{
          console.log("connected");
      }
  })
const [results, ] = await connection.execute(sql);
return results;
}
catch(err){
    console.log(err.message);
}
}
(async()=>{
    try{
        const results = await query('SELECT id, name, released_year FROM sprogramming_languages ');
        console.log(results); *for some reason this result is undefined* 
    } catch(err){
       console.log(err);
    }
}) ();

app.get('/',(req,res)=>{
    res.status(200).json({
        message:"welcome",
        app:"testdb"
    })
})

const port =3000;
app.listen(port,( )=>{
  console.log(`listening on port ${port}... `);
})

thank you so much.

0 Answers
Related