Posted at

TypeORM EntityMetadataNotFound: No metadata for "***" was found

MochaでテストしているコードでgetCustomRepositoryすると EntityMetadataNotFound: No metadata for "***" was found.が発生する


発生したエラー

     EntityMetadataNotFound: No metadata for "Application" was found.

at new EntityMetadataNotFoundError (src\error\EntityMetadataNotFoundError.ts:9:9)
at Connection.getMetadata (src\connection\Connection.ts:316:19)
at EntityManager.getCustomRepository (src\entity-manager\EntityManager.ts:781:86)
at Connection.getCustomRepository (src\connection\Connection.ts:351:29)
at Object.getCustomRepository (src\index.ts:287:55)
at Function.<anonymous> (src\models\applications.ts:155:17)
at Generator.next (<anonymous>)
at fulfilled (src\models\applications.ts:4:58)
at <anonymous>


該当するコードを追跡する

https://github.com/typeorm/typeorm/blob/master/src/connection/Connection.ts


Connection.ts

    /**

* Gets entity metadata for the given entity class or schema name.
*/

getMetadata(target: Function|EntitySchema<any>|string): EntityMetadata {
const metadata = this.findMetadata(target);
if (!metadata)
throw new EntityMetadataNotFoundError(target);

return metadata;
}



Connection.ts

    /**

* Finds exist entity metadata by the given entity class, target name or table name.
*/

protected findMetadata(target: Function|EntitySchema<any>|string): EntityMetadata|undefined {
return this.entityMetadatas.find(metadata => {
if (metadata.target === target)
return true;
if (target instanceof EntitySchema) {
return metadata.name === target.options.name;
}
if (typeof target === "string") {
if (target.indexOf(".") !== -1) {
return metadata.tablePath === target;
} else {
return metadata.name === target || metadata.tableName === target;
}
}

return false;
});
}



実際にデータがどうなっているのか?

引数のtargetとプロパティthis.entityMetadatasの値をおってみる


Connection.ts

    protected findMetadata(target: Function|EntitySchema<any>|string): EntityMetadata|undefined {

+ console.log("target", target);
+ console.log("this.entityMetadatas", this.entityMetadatas);


target

class Application {

}


this.entityMetadatas

[ 

: ,
EntityMetadata {
:
target: [Function: Application],
:
},
:
]


仮説

TypeScript形式のclass Application {}とJavaScript形式Function: Applicationと名前の不一致ではないか?