Nodejs构建器类函数不是函数

时间:2018-05-16 10:41:38

标签: node.js

我有一个使用Node JS构建器的自定义类来创建一个包含可能存在或不存在的变量的类以及一个自定义函数以验证相等性(覆盖默认的equals nodejs函数)此类正在用于商业目的,因此所有实际数据都已被混淆。

module.exports = class User {
  constructor() {
    this.title = '';
    this.firstName = '';
    this.lastName = '';
    this.email = '';
    this.emailConfirmation = '';
    this.password = '';
    this.passwordConfirmation = '';
    this.addressLine1 = '';
    this.addressLine2 = '';
    this.town = '';
    this.postcode = '';
    this.customerToken = '';
    this.businessOwner = '';
    this.authToken = '';
    this.subscriptionsCount = '';
  }
  equals(jsonObject) {
    return (jsonObject.title === this.title && jsonObject.firstName === this.firstName && jsonObject.lastName === this.lastName && jsonObject.email === this.email
        && jsonObject.addressLine1 === this.addressLine1 && jsonObject.town === this.town && jsonObject.postcode === this.postcode)
  }
  createUserObject() {
    return {
      withTitle: function (n) {
        this.title = n;
        return this
      },
      withFirstName: function (n) {
        this.firstName = n;
        return this
      },
      withLastName: function (n) {
        this.lastName = n;
        return this
      },
      withEMail: function (n) {
        this.email = n;
        return this;
      },
      withEMailConfirmation: function (n) {
        this.emailConfirmation = n;
        return this;
      },
      withPassword: function (n) {
        this.password = n;
        return this;
      },
      withPasswordConfirmation: function (n) {
        this.passwordConfirmation = n;
        return this;
      },
      withAddressLine1: function (n) {
        this.addressLine1 = n;
        return this;
      },
      withAddressLine2: function (n) {
        this.addressLine2 = n;
        return this;
      },
      withTown: function (n) {
        this.town = n;
        return this;
      },
      withPostCode: function (n) {
        this.postCode = n;
        return this;
      },
      withAuthToken: function (n) {
        this.authToken = n;
        return this;
      },
      withBusinessOwner: function (n) {
        this.businessOwner = n;
        return this;
      },
      withCustomerToken: function (n) {
        this.customerToken = n;
        return this;
      },
      withSubscriptionsCount: function (n) {
        this.subscriptionsCount = n;
        return this;
      }
    }
  }
};

我使用new关键字调用我的自定义类,因此它应该实例化一个新类,然后我尝试按上面所述调用equals函数,如下所示:

代码

var myUser = new User();

myUser = myUser.createUserObject().withTitle([DATA]).withFirstName([DATA])
.withLastName([DATA]).withEMail([DATA]).withAddressLine1([DATA])
.withAddressLine2([DATA]).withTown([DATA]).withPostCode([DATA])
.withAuthToken([DATA]).withBusinessOwner([DATA])
.withCustomerToken([DATA]).withSubscriptionsCount([DATA]);

if(myUser.equals([VALIDATION-DATA])) {
  SOME ACTION HERE
}
else {
  SOME OTHER ACTION HERE, THROW ERROR
}

我的输出是通过终端窗口,因此告诉我myUser.equals is not a function如下所示:

终端

(node:8376) UnhandledPromiseRejectionWarning: TypeError: myUser.equals is not a function
at HttpRequest.get.then (/Users/sam.levene/projects/public_api_tests/custom_classes/ApiServices.js:40:28)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:8376) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:8376) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 个答案:

答案 0 :(得分:0)

您使用上下文对象(this)真的让自己绊倒了。

我不认为你在这里所做的事情是最好的方式。基本上,一旦你创建了该对象

return {
    withTitle: function (n) {
        this.title = n;
        return this
    },

您正在创建新的上下文。这就是equals未定义的原因。如果您console.log(this, this.equals)在其中一位制定者中,您就会明白我的意思。

更好的方法是:

class UserFactory
    getInstance({ title, firstname, lastname, ... })

以及实现equals

的User类
class User
    constructor({ title, firstname, lastname, ... })
    equals()