JavaScript类TypeError X不是函数

时间:2018-08-06 12:45:05

标签: javascript node.js function typeerror es6-class

这是我一段时间无法解决的问题。我正在创建一个ES6类:

const https = require('https');

class LetsGrowHandler {
    constructor() {
    }

    connectLG() {
        return new Promise(function(resolve, reject) {
            const post_options = 
                {
                    host: 'api.letsgrow.com',
                    path: '/Token',
                    method: 'POST'
                };
            const postData = require('querystring').stringify(
                {
                    username: '',
                    password: '',
                    grant_type: 'password',
                    signin: 'Sign in'
                });
            let data;
            const response = https.request(post_options, function (res) {
                res.setEncoding('utf8');
                res.on('data', function (chunk) {
                    data += chunk;
                });
                res.on('end', () => {
                    resolve(data);
                });
            });
            response.on('error', (e) => {
                reject(e);
            });
            response.write(postData);
            response.end();
        });
    }
}

module.exports = LetsGrowHandler;

然后我要通过一个简单的单元测试来测试该功能是否正常工作:

 describe('LetsGrowHandler', function () {
    describe('retrieve token', function () {
        it('connectLG should succeed', function (done) {
            // This was missing: const lgh = new LetsGrowHandler();            
            lgh.connectLG()
                .then(function (value) {
                    //console.log(value);
                    chai.assert(value.access_token !== null);
                    done();
                })
                .catch(function (err) {
                    console.log(err);
                    chai.assert(1 === 3);
                    done();
                });
        });
    });
});

不幸的是,我得到了结果

        lgh .connectLG()
             ^

TypeError: lgh.connectLG is not a function
    at Suite.<anonymous> (D:\Code\ghdb\tests\LetsGrowHandler_test.js:7:15)

Suite.<anonymous>是什么意思?那已经是问题的指针了吗?

我在这里想念什么? 谢谢, 詹斯

0 个答案:

没有答案