Home > Software design >  Node JS, MYSQL connection.query is not a function
Node JS, MYSQL connection.query is not a function

Time:05-18

db_config.js:


const mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'test'
})

connection.connect(function(err){
    if(!!err){
        console.log(err)
    }
    else{
        console.log('Connected')
    }
})

module.export = connection

Users.js

var express = require('express');
var router = express.Router();
var connection = require('../db_config')


/* GET users listing. */
router.get('/comments', function(req, res, next) {

  var getCommentsQ = "SELECT * FROM `comments`";

  connection.query(getCommentsQ, function(err, result){
    if(err){
      console.log(err)
      res.send('Unable to get the comments')
    }
    else{
      res.send(result)
    }
  })

});

module.exports = router;

Error: Whenever i go to "localhost:3000/users/comments" it says: "connection.query is not a function"

I followed this tutorial: I was following this tutorial, his worked mine not working

Warning he using Jade im using Ejs, but its not view-engine problem i think

CodePudding user response:

You have a typo in your export, try exporting your connection like this.

module.exports = connection;
  • Related