module.exports如何返回函数值?

时间:2018-01-26 11:43:47

标签: javascript node.js express sequelize.js

我正在处理节点并尝试使用module.exports。

console.log(aolCount);输出正确的值,但不会从命令return aolCount;

返回任何内容

这是我的代码。

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;
        });
    }
};

0 个答案:

没有答案