1

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

2
  • 1
    is it the module.export = connection . should be module.exports Commented May 17, 2022 at 20:50
  • Sry you were correct,but im dump and i only erase all of it and only wrote module.exports XD. Problem solved btw Commented May 17, 2022 at 21:14

1 Answer 1

2

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

module.exports = connection;
Sign up to request clarification or add additional context in comments.

1 Comment

Don't post a real answer for typos. Just comment and vote to close (or flag as a typo if you don't have enough rep to vote).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.