I want to get a return-value from this asynchronous function exports.getServices. But I just get nothing / null back.
var http = require("http");
var mysql = require('mysql');
var url = require("url");
var connection = mysql.createConnection({
...
});
connection.connect();
exports.getServices = function (){
connection.query('SELECT DISTINCT SERVICE FROM booking_count', function(err, rows, fields) {
if (err) throw err;
var services = new Array();
for (var i in rows) {
services[i] = rows[i].SERVICE;
}
return services;
});
}
I access this method from another module:
var express = require('express');
var hbs = require('express3-handlebars');
var app = express();
app.engine('html', hbs({defaultLayout: 'layout'}));
app.set('view engine', 'html');
app.set('views', __dirname + '\\views');
app.use(express.bodyParser());
var mysql = require('./mysql');
app.get('/', function(req, res) {
res.render('index',{title:"Services", services:mysql.getServices()});
});
app.get('/article/:id', function(req, res) {
var entry = blogEngine.getBlogEntry(req.params.id);
res.render('article',{title:entry.title, blog:entry});
});
app.listen(3000);
It would be very helpful if you could post a corrected code. Thank you very much in advance!