Nodejs ES6类没有返回方法

时间:2017-05-31 16:07:26

标签: node.js class ecmascript-6 promise

我正在尝试使用ES6 Classes在我正在处理的项目中构造错误处理程序。在我实例化该类之后,我尝试并记录创建的对象,希望看到我定义的两个方法constructMessagehandle,但我得到的只是构造函数生成的对象。

为什么会发生这种情况的任何想法?

'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;

然后在另一个文件中我需要它并实例化它:

'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); // ErrorHandler {
                     //  funcName: undefined,
                     //  errMsg: undefined,
                     //  statusCode: undefined,
                     //  intError: undefined,
                     //  subject: 'Error topic',
                     //  topic: 'topic' }
}

0 个答案:

没有答案