0

I am just getting to grips with node and trying to use module.exports.

console.log(aolCount); outputs the correct value, but nothing is returned from the command return aolCount;

Here is my code.

app.js

const express = require('express');
const aol = require('./queries/aol');

const app = express();
const port = process.env.PORT || 3000

app.get('/sample', function (req, res, next) {
   res.send(aol.getAOLDataCount());
});

app.listen(port);
console.log(`Server listening on port ${port}`);

aol.js

const Sequelize = require('sequelize');
const sequelize = new Sequelize('testdb', 'root', null, {
    host: 'localhost',
    dialect: 'mysql',
    pool:{
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    },
    define: {
        timestamps: false
    }
});

let WeeklyData = require('../../models/weekly_data');

sequelize
    .authenticate()
    .then(() => {
        console.log('Connection has been established to the Database');
    })
    .catch(err => {
        console.error('Unable to connect to the Database', err);
    });

module.exports = {
    getAOLDataCount:  function () {
        wd = new WeeklyData(sequelize, Sequelize.DataTypes);
        wd.count({where: {week:201740, project: 8}}).then(function (aolCount) {
            console.log(aolCount);
            return aolCount;
        });
    }
};
Yury Tarabanko
  • 39,619
  • 8
  • 73
  • 90
PrestonDocks
  • 3,706
  • 8
  • 33
  • 59
  • @YuryTarabanko I think this is different, because my module.exports may and will eventually contain many elements in the object that it exports. – PrestonDocks Jan 26 '18 at 11:50
  • No, it is 100% duplicate. You are trying to return from async call. This has nothing to do with exports. – Yury Tarabanko Jan 26 '18 at 11:58
  • @PrestonDocks It's about returning from the `getAOLDataCount` function. That it is part of an export does not matter. – Bergi Jan 26 '18 at 12:00
  • @PresonDocks I typed up my answer right before it got flagged as a duplicate so no reason to waste it, hope this helps: https://gist.github.com/travispaul/a83a79c2936a26850637c0aefdd26c35 – functionvoid Jan 26 '18 at 12:04
  • @hyphenthis Such a shame that your answer can not be left for this question. Your answer gives a brilliant explanation of Async functions and has furthered my knowledge considerably. – PrestonDocks Jan 26 '18 at 12:20

0 Answers0