javascript - module.exports: is not a function - Stack Overflow

Following is my sqlhelper.jsvar req = require("request");var tp = require('tedious-prom

Following is my sqlhelper.js

var req = require("request");
var tp = require('tedious-promises');
var dbConfig = require('../config/connectionString.json');
var TYPES = require('tedious').TYPES;

function SQLHelper() {
}

SQLHelper.prototype.ExecuteDataset = function(query, params, callback, failure) {

    tp.setConnectionConfig(dbConfig);
    tp.sql(q);
    $(params).each(function(idx) {
        var p = params[idx];
        tp.parameter(p.name, p.value);
    });

    tp.execute()
        .then(function(results) {
            callback(results)
        }).fail(function(err) {
            failure(err);
        });
};

module.exports = SQLHelper;

I have used it like this

var sqlHelper = require('../SQLHelper.js');
sqlHelper.ExecuteDataset(q, params, function(results) {//Here I get error
                console.log(results)
            },
            function(err) {
                console.log(err);
            });

I get following error TypeError: sqlHelper.ExecuteDataset is not a function

I dont know whats wrong here. Kindly help.

Following is my sqlhelper.js

var req = require("request");
var tp = require('tedious-promises');
var dbConfig = require('../config/connectionString.json');
var TYPES = require('tedious').TYPES;

function SQLHelper() {
}

SQLHelper.prototype.ExecuteDataset = function(query, params, callback, failure) {

    tp.setConnectionConfig(dbConfig);
    tp.sql(q);
    $(params).each(function(idx) {
        var p = params[idx];
        tp.parameter(p.name, p.value);
    });

    tp.execute()
        .then(function(results) {
            callback(results)
        }).fail(function(err) {
            failure(err);
        });
};

module.exports = SQLHelper;

I have used it like this

var sqlHelper = require('../SQLHelper.js');
sqlHelper.ExecuteDataset(q, params, function(results) {//Here I get error
                console.log(results)
            },
            function(err) {
                console.log(err);
            });

I get following error TypeError: sqlHelper.ExecuteDataset is not a function

I dont know whats wrong here. Kindly help.

Share Improve this question asked Dec 3, 2016 at 16:00 MARKAND BhattMARKAND Bhatt 2,65011 gold badges51 silver badges86 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You have to create an instance of SQLHelper before it will have the prototype methods. Prototype properties are available on an instance of the object, not on the constructor.

If you want a singleton (only one object that every one shares), you can do this:

// export an instance of our object
module.exports = new SQLHelper();

Or, if you want a separate object each time you use it, you can change where you use it to this:

// load module and create an instance
let sqlHelper = new (require('../SQLHelper.js'))();

Or, if you don't actually have any instance data and you just want to use a namespace, you can do this:

let SQLHelper = {};
SQLHelper.ExecuteDataset  = ...

module.exports = SQLHelper;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745447549a4628117.html

相关推荐

  • javascript - module.exports: is not a function - Stack Overflow

    Following is my sqlhelper.jsvar req = require("request");var tp = require('tedious-prom

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信