Entity metadata for Role#users was not found

≡放荡痞女 提交于 2020-03-23 14:12:08

问题


Trying to make a OneToMany and ManyToOne relationship with typeorm but i get this error, i don't know what's wrong with my code.

i have a User table:

import { BaseEntity, Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Field, ID, ObjectType } from 'type-graphql';

import { Role } from './';

@ObjectType()
@Entity()
export class User extends BaseEntity {
    @Field(() => ID)
    @PrimaryGeneratedColumn()
    public id: number;

    @Field()
    @Column('text', { unique: true })
    public userName: string;

    @Column()
    public password: string;

    @Field()
    @Column('boolean', { default: true })
    public isActive: boolean;

    @ManyToOne(() => Role, role => role.users)
    @Field(() => Role, { nullable: true })
    public role: Role;
}

and a role table:

import { BaseEntity, Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { Field, ID, ObjectType } from 'type-graphql';

import { User } from '.';

@ObjectType()
@Entity()
export class Role extends BaseEntity {
    @Field(() => ID)
    @PrimaryGeneratedColumn()
    public id: number;

    @Field()
    @Column('text', { unique: true })
    public name: string;

    @OneToMany(() => User, user => user.role, { lazy: false })
    @Field(() => [User], { nullable: true })
    public users: User[];
}

However i keep getting this error

(node:4541) UnhandledPromiseRejectionWarning: Error: Entity metadata for Role#users was not found. Check if you specified a correct entity object and if it's connected in the connection options. [1] at /Users/soufiane/projects/pos/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:571:23 [1] at Array.forEach () [1] at EntityMetadataBuilder.computeInverseProperties (/Users/soufiane/projects/pos/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:567:34) [1] at /Users/soufiane/projects/pos/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:80:74 [1] at Array.forEach () [1] at EntityMetadataBuilder.build (/Users/soufiane/projects/pos/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:80:25) [1] at ConnectionMetadataBuilder.buildEntityMetadatas (/Users/soufiane/projects/pos/node_modules/typeorm/connection/ConnectionMetadataBuilder.js:57:141) [1] at Connection.buildMetadatas (/Users/soufiane/projects/pos/node_modules/typeorm/connection/Connection.js:494:57) [1] at Connection. (/Users/soufiane/projects/pos/node_modules/typeorm/connection/Connection.js:126:30) [1] at step (/Users/soufiane/projects/pos/node_modules/tslib/tslib.js:136:27) [1] (node:4541) 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) [1] (node:4541) [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:


This just means that your entities are either not loading, or are loading incorrectly

You need to fix the loading of your entities. Entities will usually be loaded from the ormconfig file

Just create a file ormconfig.js and type something like this, emphasis on the entities

module.exports = {
  "name": "default",
  "type": "mongodb",
  "host": "localhost",
  "port": "27017",
  "username": "root",
  "password": "",
  "database": "rocketlaunches",
  "entities": [
    __dirname + "entities/**/*.entity.ts"
  ]
}

Now obviously, you would load your entities from wherever they are on your project




回答2:


My problem was that I was using node-ts to run only the typescript files for my project. However, my build still emitted .js files that were ignored/hidden. I don't know why, but cleaning out the *.js and *.js.map compiled files worked. What's strange is my ormconfig only specifies .ts files.

I recommend adding the "noEmit": true to tsconfig.json compiler options to prevent any future headaches with a similar approach.



来源:https://stackoverflow.com/questions/56693067/entity-metadata-for-roleusers-was-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!