typeorm - 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);
解决方案
推荐阅读
- php - 有没有办法将此 RAW 查询转换为 Eloquent?
- javascript - 生成图像时谷歌图表注释移到后面
- c# - 从 C# 中,如何设置默认 Crystal Report 参数但仍然在参数对话框中看到此参数
- mysql - MySQL服务器版本10.3.9-MariaDB不能查询文本有单引号
- reactjs - 如何使用 react-select 在每个下拉项下方自定义呈现子文本?
- python - 如何将假人添加到 Pandas DataFrame?
- python - 绘制一条线或一系列线并获得组合线的总长度?
- verilog - 阻塞和非阻塞语句的verilog内部延迟
- javascript - 一种在 React 中使用 localstorage 显示组件一次的方法
- c# - 如何使用 Entity Framework 6 从 10 多个表中获取记录并从中显示报告?