簡體   English   中英

TypeORM 中的 ColumnTypeUndefinedError

[英]ColumnTypeUndefinedError in TypeORM

有人能告訴我為什么,在啟動我的項目時,我得到這個錯誤:

ColumnTypeUndefinedError: Column type for Order#author is not defined and cannot be guessed. Make sure you have turned on an "emitDecoratorMetadata": true option in tsconfig.json. Also make sure you have imported "reflect-metadata" on top of the main entry file in your application (before any entity imported).If you are using JavaScript instead of TypeScript you must explicitly provide a column type.

這是我上面的專欄,

    @Column({
        nullable: true
    })
    author: User;

謝謝你的幫助!

這是因為數據庫不知道User類型。

User更改為string ,錯誤將 go 消失,因為數據庫知道string

@Column({
        nullable: true
    })
    author: string;

如果您想要User而不是string ,請使用 @ManyToOne 而不是 @Column。 這將設置與 User 表的外鍵關系,如果您添加eager: true ,則當您使用任何 TypeOrm find*方法時, User實體將與基本實體一起自動加載:

@ManyToOne(() => User, { eager: true })
author: User;

您收到此錯誤是因為您的tsconfig.json文件配置不正確。 將以下內容添加到您的文件中。

{
    "emitDecoratorMetadata": true,
    "lib": ["es5", "es6", "dom"],  
    "moduleResolution": "node",
    "target": "es5", 
}

那么你必須運行

npm運行tsc

編輯完 tsconfig 文件后,進行重新編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM