Hi, I am trying to build an MVC model API. I'm having troubles making queries. It seems like I don't export my connection variable correctly, cause I've got a "connection.query is not a function" error.
I've tried to export it using exports.module but as I already have router to export, I had a router.use() error. Here is index.js : var express = require('express'); var router = express.Router(); var mysql = require('mysql'); var indexController = require('../controllers/IndexController'); var booksController = require('../controllers/BooksController'); connection = mysql.createConnection({ debug:true, host:'localhost', user:'user', password:'password', database:'database' }); connection.connect(function(error){ if(!!error){ console.log(error); }else{ console.log('Connected!:)'); } }); router.get('/', indexController.index); router.get('/books', booksController.books); module.exports = router; Here is booksController.js : var BooksModel = require('../models/BooksModel'); exports.books = async function(req, res, next){ res.send(await BooksModel.getAllBooks(req, res)) } Here is booksModel.js : var connection = require('../routes/index'); async function getAllBooks(req, res){ return await connection.query("select * from books", function (error,results, fields) { if (error) throw error; res.json(results); }); }); } module.exports={ getAllBooks } I expect to get datas coming from my database, according to the query request. Any help would be really appreciated. Thanks ! -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. To post to this group, send email to nodejs@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/7306b5a7-0cc9-4eb3-9c5b-be603717e181%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.