volume one
volume one

Reputation: 7581

Why is this not a function?

I have a file called offer.js which does this (it outputs data fine);

function rsOffersAll() {
    global.MSSQL_MYDB.connect().then(function(error, result) {
        global.MSSQL_MYDB.request(global.MSSQL_MYDB).query('SELECT Top(10) * FROM [Broadcast].[Offer]').then(function (result) {
            console.dir(result);
            return result;
        });
    });
}

module.exports.rsOffersAll = rsOffersAll();

In my router file, I have this:

const Offer = require('../models/offer');
Offer.rsOffersAll();

If I run the above I get this error:

Offer.rsOffersAll();
      ^

TypeError: Offer.rsOffersAll is not a function

Why is it not a function? And how do I execute that function so that I can get the data into another file?

Upvotes: 0

Views: 74

Answers (1)

Quentin
Quentin

Reputation: 944526

rsOffersAll() evaluates to the return value you get when you call the rsOffersAll function.

That function has no return statement, so it returns undefined.

undefined is not a function.

If you want to export the function, then don't call it on the line you are trying to export from.

module.exports.rsOffersAll = rsOffersAll;

Upvotes: 1

Related Questions