2

I use Node.js 7.44.

In file user.models.ts, I have multiple classes, like:

import { Exclude, Expose } from "class-transformer";

export class User {
    @Expose() _id: string;
    @Expose() fname: string;
    @Expose() lname: string;
    @Expose() birthday: Date;
    @Expose() address: Address;

    constructor(){...}
}

@Exclude()
export class Address {
    @Expose() street: string;
    @Expose() num: number;
    @Expose() city: string;
    @Expose() zipCode: number;
}

When I compile the server, I get the error:

ReferenceError: Address is not defined

How can I use multiple classes in the same file?

3
  • 1
    First define the class and then use it. Because concept of hoisting is not applicable on the classes. Commented May 19, 2017 at 9:48
  • note: i think you are supposed to export juste one object per file, see the answer here: stackoverflow.com/questions/41228221/… Commented May 19, 2017 at 9:52
  • Shubham thx it really work! Commented May 19, 2017 at 10:06

1 Answer 1

1

Since you're using Address in User, you have to define Address first. Consider using the eslint rule no-use-before-define.

import { Exclude, Expose } from "class-transformer";

@Exclude()
export class Address {
    @Expose() street: string;
    @Expose() num: number;
    @Expose() city: string;
    @Expose() zipCode: number;
}

export class User {
    @Expose() _id: string;
    @Expose() fname: string;
    @Expose() lname: string;
    @Expose() birthday: Date;   
    @Expose() address: Address;

    constructor(){...}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.