Wednesday, 31 May 2017

Nodejs ES6 class not returning methods

I am trying to use ES6 Classes to construct an error handler in a project I'm working on. After I instantiate the class I try and log the object that is created, hoping to see the two methods I defined constructMessage and handle, but all I get is the object generated by the constructor function.

Any ideas why this could be happening?

'use strict';

const snsHandler = require('./../sns/sns-handler');// snsHandler returns a promise

class ErrorHandler {
  constructor (errObject, snsTopic, snsSubject) {
    this.funcName = errObject.functionName;
    this.errMsg = errObject.errorMessage;
    this.statusCode = errObject.statusCode;
    this.intError = errObject.internalError;
    this.subject = snsSubject;
    this.topic = snsTopic;
  }

  constructMessage () {
    let errorObject = {
      "message": this.errMsg,
      "statusCode": this.statusCode,
      "functionName": this.funcName,
      "internalError": this.intError
    }
    errorObject = JSON.stringify(errorObject);
    return errorObject;
  }

  handle () {
    return snsHandler.manageSns(this.topic, this.constructMessage(), this.subject); // snsHandler returns a promise
  }
}

module.exports = ErrorHandler;

Then in another file I require it and instantiate it:

'use strict';

const errHandler = require('./lib/modules/errorHandler/error-handler');
const topicName = 'topic';
const snsSubject = 'Error topic';

const simulatePurchase = () => {
  const _err = new errHandler({}, topicName, snsSubject);
  console.log(_err);
}



via hyprstack

No comments:

Post a Comment