Creating multiple relationships in TypeORM for entities with private properties

In my node application, I am utilizing the typeorm library for entity mapping. My goal is to establish multiple type relations between entities. While following the documentation, I noticed that the entity properties are marked as public, allowing access from other entities. However, I have defined my entity properties as private. To circumvent this, I have implemented a getProperty method within the entity to retrieve the property on the entity instance. But now, typeorm is throwing an error stating that

entity.getProperty is not a function
.

User Entity

@Entity()
class User {

    private USER_ID_PREFIX: string = "uid-";

    @PrimaryColumn()
    private id: string;
    @Column({nullable: false, length: 15})
    private firstname: string;
    @Column({nullable: false, length: 15})
    private lastname: string;
    @Column({nullable: false, unique: true, length: 25})
    private email: string;
    @Column({nullable: false})
    private password: string;
    @OneToMany(() => Role, (role) => role.getProperty(EntityProperty.USER))
    private roles: Role[];

    public constructor(...args: any[]) {
        if (args.length === 0)
            return;
        if (args.length === 1) {
            if (args[0] instanceof UserCreateRequestDto) {
                let requestDto = args[0];
                this.id = this.USER_ID_PREFIX + randomUUID();
                this.firstname = requestDto.getProperty(DtoProperty.FIRSTNAME);
                this.lastname = requestDto.getProperty(DtoProperty.LASTNAME);
                this.email = requestDto.getProperty(DtoProperty.EMAIL);
                this.password = requestDto.getProperty(DtoProperty.PASSWORD);
            }
        }
    }

    public getProperty(property: EntityProperty): any {
        if (property === EntityProperty.ID)
            return this.id;
        if (property === EntityProperty.FIRSTNAME)
            return this.firstname;
        if (property === EntityProperty.LASTNAME)
            return this.lastname;
        if (property === EntityProperty.EMAIL)
            return this.email;
        if (property === EntityProperty.PASSWORD)
            return this.password;
        if (property === EntityProperty.ROLES)
            return this.roles;
        throw new Error(`No such entity property as ${property.toString()} exists in User`);
    }
}

export default User;

Role Entity

@Entity()
class Role {

    private ROLE_ID_PREFIX: string = "rid-";

    @PrimaryColumn()
    private id: string;
    @Column({nullable: false})
    private role: string;
    @ManyToOne(() => User, (user) => user.getProperty(EntityProperty.ROLES))
    private user: User;

    public constructor(...args: any[]) {
        if (args.length === 0)
            return;
        if (args.length === 1) {
            if (args[0] instanceof RoleCreateRequestDto) {
                let requestDto = args[0];
                this.id = this.ROLE_ID_PREFIX + randomUUID();
                this.role = requestDto.getProperty(DtoProperty.ROLE);
            }
        }
    }

    public getProperty(property: EntityProperty): any {
        if (property === EntityProperty.ID)
            return this.id;
        if (property === EntityProperty.ROLE)
            return this.role;;
        if (property === EntityProperty.USER)
            return this.user;
        throw new Error(`No such entity property as ${property.toString()} exists in Role`);
    }
}

export default Role;

Error stack

query: SELECT VERSION() AS `version`
Error initializing mysql datasource TypeError: user.getProperty is not a function
    at RelationMetadata.givenInverseSidePropertyFactory (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\src\entities\Role.ts:22:43)
    at RelationMetadata.buildInverseSidePropertyPath (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata\RelationMetadata.ts:628:29)
    at D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata-builder\EntityMetadataBuilder.ts:1116:26
    at Array.forEach (<anonymous>)
    at EntityMetadataBuilder.computeInverseProperties (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata-builder\EntityMetadataBuilder.ts:1096:34)
    at D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata-builder\EntityMetadataBuilder.ts:158:18
    at Array.forEach (<anonymous>)
    at EntityMetadataBuilder.build (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata-builder\EntityMetadataBuilder.ts:157:25)
    at ConnectionMetadataBuilder.buildEntityMetadatas (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\connection\ConnectionMetadataBuilder.ts:106:11)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Answer №1

When working with TypeORM, it is essential for the properties to be declared as public. This is because TypeORM requires direct access to these properties, but in your current code, the entity properties are marked as private. To resolve this issue, consider updating your code as follows:

@Entity()
class User {
    @PrimaryColumn()
    public id: string;

    @Column({ nullable: false, length: 15 })
    public firstname: string;

    @Column({ nullable: false, length: 15 })
    public lastname: string;

    @Column({ nullable: false, unique: true, length: 25 })
    public email: string;

    @Column({ nullable: false })
    public password: string;

    @OneToMany(() => Role, (role) => role.user)
    public roles: Role[];

    // Add the remaining code here...
}

@Entity()
class Role {
    @PrimaryColumn()
    public id: string;

    @Column({ nullable: false })
    public role: string;

    @ManyToOne(() => User, (user) => user.roles)
    public user: User;

    // Include the rest of the code...
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Firebase Cloud Function Local Emulator Fails to Retrieve Data with Error 404

My goal is to locally trigger a Firebase Cloud Function using the emulator. However, every time I try, the function returns a 404 Not Found status code and a response body of Cannot Get. The function is deployed locally and visible on the UI, but it fails ...

Encountering a NullInjectorError in Angular while utilizing dynamic module federation when importing a standalone component along with

My main goal is to develop a shell application acting as a dashboard without routing, featuring multiple cards with remote content (microfrontend standalone component). I have been following a tutorial that aligns closely with my requirements: . The reas ...

No information is being emitted by the subject

In my application, I have a feature where users input data that needs to be validated in a form. Once the validation is successful, a button is enabled allowing the user to submit their order. However, I'm facing an issue with this specific component ...

What is the best way to create a distinct slug using TypeScript and mongoose?

After scouring through numerous modules, I couldn't find one that worked with TypeScript, including mongoose-slug-generator and mongoose-slug-plugin. ...

What is the best way to save objects in the store (ngrx, ngxs) while preserving their methods functionality?

As I delve into the Redux pattern, I realize the importance of storing only plain objects in the Store. However, I find myself wanting to use more complex objects with methods like "hasParent", "isReadonly", and "isValid" in my application. While ngrx all ...

Perform an HTTP GET request to a RESTful service with specified parameters using Angular 2

I'm currently facing an issue while attempting to create a GET request to the YouTube Web API. I'm struggling with passing parameters through the http.get function and have confirmed the request is being sent using Fiddler. However, I keep receiv ...

Challenges with incorporating asynchronously fetched data in component operations

I have encountered an issue where the data retrieved from a server in a service is available in the component's template but not in the actual code. This seems strange to me. I made the call in the ngOnInit method of my main AppComponent ngOnInit() { ...

Information obtained from the visible is consistently indefinable

I provide a service that returns observables of an array of objects allItems: Item[] = [ { id: "1", name: "item 1" }, { id: "2", name: "item 2" }, { id: "3" ...

How to turn off automatic password suggestions in Chrome and Firefox

Currently, I have integrated a 'change password' feature which includes fields for 'old password', 'new password', and 'retype password'. However, the autocomplete feature is suggesting passwords from other user acco ...

What's the best way to maintain the return type of a function as Promise<MyObject[]> when using forEach method?

I am currently working with a function called search, which at the moment is set up to return a type of Promise<MyObject[]>: export function search(args: SearchInput) { return SomeInterface.performSearch(args) .then(xmlRequest =&g ...

Alert: Attempting to access an undefined value in an indexed type

I would like to find a way in Typescript to create a hashmap with indexable types that includes a warning when the value could potentially be undefined during a lookup. Is there a solution for this issue? interface HashMap { [index: number]: string; } ...

Struggling to launch on Vercel and encountering the error message, """is not allowed by Access-Control-Allow-Origin. Status code: 204""

Greetings! I trust you are doing well. Currently, I am engrossed in developing a full-stack application. The app runs smoothly on localhost without any issues. However, upon deploying both the server and front end on Vercel, a snag arose when attempting to ...

Tips for creating an input box that only accepts floating point numbers:

I have a custom component - a text box that I am using in two different places. In one location, it accepts integers and in another, floats. For the integer validation (where dataType=2), I have successfully implemented it. However, for the float validat ...

Selecting ion-tabs causes the margin-top of scroll-content to be destroyed

Check out the Stackblitz Demo I'm encountering a major issue with the Navigation of Tabs. On my main page (without Tabs), there are simple buttons that pass different navparams to pre-select a specific tab. If you take a look at the demo and click t ...

Typescript: The .ts file does not recognize the definition of XMLHttpRequest

I have encountered an issue with a .ts file containing the following code: var xhttp = new XMLHttpRequest(); After running the grunt task to compile the ts files using typescript, no errors were reported. However, when I attempt to instantiate the class ...

Angular 11 Working with template-driven model within a directive

My currency directive in Angular 8.2 formats currency fields for users by using the following code: <input [(ngModel)]="currentEmployment.monthlyIncome" currency> @Directive({ selector: '[ngModel][currency]', providers: [Curr ...

Creating a Variety of Files in the Angular Compilation Process

Currently, I am developing an Angular project and faced with the task of creating various files during the build process depending on certain conditions or setups. I would appreciate any advice on how to accomplish this within the Angular framework. I att ...

The 'SVGResize' or 'onresize' property is not available on the 'SVGProps<SVGSVGElement>' type

Using React with SVG I'm facing an issue with handling the resizing event of an svg element. I have looked into using the SVGResize and onresize events, but encountered compilation errors when trying to implement them: const msg1 = (e: any) => co ...

Is it possible to access NgbdModalContent properties from a different component?

If I have a component with a template containing an Edit button. When the user clicks on it, I want to load another component as a dynamic modal template. The component is named ProfilePictureModalComponent and it includes the Edit button: (Angular code h ...

How can we prevent users from changing URLs or accessing pages directly in Angular 7 without using authguard?

Hey there! I am trying to find a way to prevent users from accessing different pages by changing the URL, like in this scenario. Is there a method that can redirect the user back to the same page without using Authguard or any Modules? I have a StackBlit ...