Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

446
Views
Node JS, MYSQL connection.query is not a function

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

module.exports = connection;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!