首页 > 解决方案 > typeorm getConnection 找不到实体,但可以在 createConnection 上找到

问题描述

尝试使用 getConnection 时出现错误 RepositoryNotFoundError: No repository for "User" was found. Looks like this entity is not registered in current "default" connection? at RepositoryNotFoundError.TypeORMError [as constructor]

但是,用户实体文件位于该文件夹中,并且该文件夹与该文件处于同一级别。

如果我尝试在 .then 回调中获取存储库,则没有问题。

// Works
createConnection({
    "type": "mysql",
    "host": process.env.DB_HOST,
    "port": 3306,
    "username": process.env.DB_USER,
    "password": process.env.DB_PASSWORD,
    "database": process.env.DB_NAME,
    "synchronize": true,
    "entities": [
        __dirname + "/entities/*.js"
    ]
}).then(connection => {
    connection.getRepository(User)
});

// Does not work
const connection = getConnection();
connection.getRepository(User);

标签: typeorm

解决方案


推荐阅读