Entity Metadata Not Found: No metadata for "User" was found.
See original GitHub issueIssue type:
[ ] question [X] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
I’m having a similar issue covered in #420 however the solution did not apply here.
// Database.ts
import { createConnection } from "typeorm";
import { User } from "../app/models/User.model";
import { config, DIALECT } from "../config";
export const Connection = createConnection({
database: config.DATABASE.DB,
entities: [
User
],
host: config.DATABASE.SERVER,
logging: true,
password: config.DATABASE.PASSWORD,
port: config.DATABASE.PORT_DB,
synchronize: true,
type: DIALECT,
username: config.DATABASE.USER_DB,
});
// User.model.ts
import { BaseEntity, BeforeInsert, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { Contains, IsInt, IsString, Length, IsEmail, IsFQDN, IsDate, Min, Max, IsBoolean } from "class-validator";
import * as bcrypt from 'bcrypt';
@Entity("user")
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
public id: number;
@Column()
@IsEmail()
public email: string
@Column()
@IsString()
public password: string
@Column({
type: "enum",
enum: [
"student",
"tutor"
]
})
public type: string
@Column()
@IsString()
public firstName: string;
@Column()
@IsString()
public lastName: string;
@Column({
type: "enum",
enum: [
"Male",
"Female",
"Other"
]
})
@IsString()
public gender: string;
@Column()
@IsString()
public phone: string;
@Column()
@IsBoolean()
public verified: boolean;
@BeforeInsert()
encryptPassword() {
this.password = bcrypt.hashSync(this.password, 10);
}
}
// User.repository.ts
import { EntityRepository, Repository } from "typeorm";
import { User } from "../models/User.model";
@EntityRepository(User)
export class UserRepository extends Repository<User> {
}
// User.controller.ts
export class UserController {
public static userRepository: UserRepository = getCustomRepository(UserRepository);
constructor() {
}
}
This is the error with that is thrown:
/Users/john/Documents/Work/PROJECT/src/error/EntityMetadataNotFoundError.ts:9
super();
^
EntityMetadataNotFound: No metadata for "User" was found.
at new EntityMetadataNotFoundError (/Users/john/Documents/Work/PROJECT/src/error/EntityMetadataNotFoundError.ts:9:9)
at Connection.getMetadata (/Users/john/Documents/Work/PROJECT/src/connection/Connection.ts:313:19)
at EntityManager.getCustomRepository (/Users/john/Documents/Work/PROJECT/src/entity-manager/EntityManager.ts:753:86)
at Connection.getCustomRepository (/Users/john/Documents/Work/PROJECT/src/connection/Connection.ts:348:29)
at Object.getCustomRepository (/Users/john/Documents/Work/PROJECT/src/index.ts:286:55)
at Object.<anonymous> (/Users/john/Documents/Work/PROJECT/app/controllers/User.controller.ts:9:52)
at Module._compile (module.js:652:30)
at Module.m._compile (/Users/john/Documents/Work/PROJECT/node_modules/ts-node/src/index.ts:400:23)
at Module._extensions..js (module.js:663:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/john/Documents/Work/PROJECT/node_modules/ts-node/src/index.ts:403:12)
It seems getCustomRepository(UserRepository); is what is causing the error, and it can’t get User’s metadata.
In issue #420 it seems like the solution was to fix all import statements for the entity, and I’ve done that. Another proposed solution is to make sure your entity is listed in your database connection, and it is.
I’m new to typeorm, however I’m really liking it compared to Sequelize, I love how typeorm integrates with TypeScript with such ease. I’m prototyping both right now for a new project and I really don’t want to have to use Sequelize. 😀
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Nevermind, I figured it out, it turns out in my index.ts file I imported my route.ts file, and the route instantiated the UserController before the connection was made, so the user entity didn’t exist in the connection, because the connection wasn’t made yet.
So I just made my route configuration a function that I called after I connected, and all works well!
I’ll close this now, gosh I feel stupid haha. Great project you guys have here.
@havenchyk In this case, I believe
class-validator
is used at run-time together withValidationPipe
. Basically it’s a way to check if the data sent to the route is astring
for that specific property.