我正在尝试使用TypeORM来使basic setup正常工作,并在设置后出现此错误。
这是一个REPL(只需yarn install && yarn db:dev
后跟yarn db:migrate && yarn start
即可重现错误)
Inserting a new user into the database...
{ EntityMetadataNotFound: No metadata for "User" was found.
at new EntityMetadataNotFoundError (/Users/admin/work/typeorm-naming-strategy/src/error/EntityMetadataNotFoundError.ts:9:9)
at Connection.getMetadata (/Users/admin/work/typeorm-naming-strategy/src/connection/Connection.ts:313:19)
at /Users/admin/work/typeorm-naming-strategy/src/persistence/EntityPersistExecutor.ts:77:55
at Array.forEach (<anonymous>)
at EntityPersistExecutor.<anonymous> (/Users/admin/work/typeorm-naming-strategy/src/persistence/EntityPersistExecutor.ts:71:30)
at step (/Users/admin/work/typeorm-naming-strategy/node_modules/typeorm/persistence/EntityPersistExecutor.js:32:23)
at Object.next (/Users/admin/work/typeorm-naming-strategy/node_modules/typeorm/persistence/EntityPersistExecutor.js:13:53)
at /Users/admin/work/typeorm-naming-strategy/node_modules/typeorm/persistence/EntityPersistExecutor.js:7:71
at new Promise (<anonymous>)
at __awaiter (/Users/admin/work/typeorm-naming-strategy/node_modules/typeorm/persistence/EntityPersistExecutor.js:3:12)
name: 'EntityMetadataNotFound',
message: 'No metadata for "User" was found.' }
答案 0 :(得分:4)
问题出在ormConfig
请尝试使用此功能:
entities: [__dirname + '/../**/*.entity.{js,ts}']
答案 1 :(得分:3)
延迟添加答案位,但这是很常见的错误。
出现上述错误的主要原因有两个。在OrmConfig中,
entities: [__dirname + '/../**/*.entity.ts'] <-- Wrong
entities: [__dirname + '/../**/*.entity.js']
答案 2 :(得分:2)
以下操作将确保TypeORM使用的文件扩展名都是.js和.ts
entities: [__dirname + '/../**/*.entity.{js,ts}']
在您的配置文件中更改它。
答案 3 :(得分:0)
我认为我的问题是由于意外地同时拥有ormconfig.json和ormconfig.ts
引起的答案 4 :(得分:0)
我的失败是我没有使用模型,而是使用了请求。请求较早加载,因此未定义结束。尝试将您的模型类移至模型
答案 5 :(得分:0)
就我而言,我忘记将新实体(您的情况下为User)添加到app.module.ts中。 这是对我有用的解决方案:
import React, {useState} from 'react'
import Select from 'react-select'
const unitOptions = [
{value: 'metric', label: 'Celcius'},
{value: 'imperial', label: 'Fahrenheit'}
];
function Dashboard() {
const [units, setUnits] = useState("");
const [city, setCity] = useState("");
function selectUnits() {
const units = document.getElementById("unitSelect").value;
console.log(units);
}
function selectCity(event) {
const cityName = event.target.value;
const targetName = event.target.name;
}
return(
<div>
<form className="inputArea">
<Select
id="unitSelect"
options={unitOptions}
className="unitSelect"
placeholder="Units"
onChange={selectUnits}
/>
</div>