繁体   English   中英

RepositoryNotFoundError : TypeORM

[英]RepositoryNotFoundError : TypeORM

我正在尝试使以下示例正常工作:

https://github.com/typeorm/javascript-example/tree/master/src/app3-es6

我遇到了以下错误:

Error
    at new RepositoryNotFoundError (...\node_modules\typeorm\connection\error\RepositoryNotFoundError.js:24:23)
    at Connection.findRepositoryAggregator (...\node_modules\typeorm\connection\Connection.js:513:19)
    at Connection.getRepository (...\node_modules\typeorm\connection\Connection.js:405:21)
    at ...\index.js:27:37
name: 'RepositoryNotFoundError',
  message: 'No repository for "Post" was found. Looks like this entity is not registered in current "default" connection?'

这是 index.js

const typeorm = require("typeorm"); // import * as typeorm from "typeorm";
const Post = require("./model/Post"); // import {Post} from "./model/Post";
// import Post from './model/Post.js';
const Category = require("./model/Category"); // import {Category} from "./model/Category";

typeorm.createConnection({
    driver: {
        type: "oracle",
        host: "localhost",
        port: 1521,
        username: "uname",
        password: "pwd",
        sid: "dev"
    },
    entities: [
        __dirname + "/entity/*.js"
    ],
    autoSchemaSync: true
}).then(function (connection) {
    console.log(connection);

    let post = new Post.Post();
    post.title = "Control flow based type analysis";
    post.text = "TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.";
    post.categories = [new Category.Category(0, "TypeScript"), new Category.Category(0, "Programming")];

    let postRepository = connection.getRepository(Post.Post);
    postRepository.persist(post)
        .then(function(savedPost) {
            console.log("Post has been saved: ", savedPost);
            console.log("Now lets load all posts: ");

            return postRepository.find();
        })
        .then(function(allPosts) {
            console.log("All posts: ", allPosts);
        });
}).catch(function(error) {
    console.log("Error: ", error);
});

/model/ 中的 Post.js

/*export */ class Post {
    constructor(id, title, text, categories) {
        this.id = id;
        this.title = title;
        this.text = text;
        this.categories = categories;
    }
}

module.exports = {
    Post: Post
};

分类.js

/*export */ class Category {
    constructor(id, name) {
        this.id = id;
        this.name = name;
    }
}

module.exports = {
    Category: Category
};

/entity/ 中的 PostSchema.js

const Post = require("../model/Post"); // import {Post} from "../model/Post";
const Category = require("../model/Category"); // import {Category} from "../model/Category";
const PostSchema = {
    target: Post,
    columns: {
        id: {
            primary: true,
            type: "int",
            generated: true
        },
        title: {
            type: "string"
        },
        text: {
            type: "text"
        }
    },
    relations: {
        categories: {
            target: Category,
            type: "many-to-many",
            joinTable: true,
            cascadeInsert: true
        }
    }
};

module.exports = {
    PostSchema: PostSchema
};

类别架构.js

const Category = require("../model/Category"); // import {Category} from "../model/Category";
const CategorySchema = {
    target: Category,
    columns: {
        id: {
            primary: true,
            type: "int",
            generated: true
        },
        name: {
            type: "string"
        }
    }
};

module.exports = {
    CategorySchema: CategorySchema
};

我不知道我做错了什么

看起来您的实体导入不起作用。 如果通过通配符导入:

entities: [
    __dirname + "/entity/*.js"
],`

确保您的模型已编译为 js。 你也可以只导入

createConnection({ 
    ..., 
    entities: [
        Post,
        ...
    ],}).then(...)

对于那些正在使用打字稿并遇到此问题的人:请注意,在指定实体路径时,您需要同时包含tsjs文件后缀:

  • ts使用时与本地运行ts-node
  • js使用时为经由内置的生产tsc

代码:

import * as path from 'path';
// ...
entities: [
    // assuming _dirname is your project root
    path.resolve(__dirname, '**/*.entity{.ts,.js}'),
],

几个月来我遇到了同样的问题,终于弄清楚我做错了什么。
导入实体时,请确保文件名完全匹配。 它不会抛出任何错误,但在运行时,它会抛出上述错误。
例如。 在实体或模型类中,如果我们这样导入,

import { FooClass } from "./foo-Class.model";

它不同于

import { FooClass } from "./foo-class.model";

它不会显示任何错误,但是当您尝试调用该表时,它会显示完全相同的错误。

我有同样的问题。 没有一个解决方案对我有用。 经过大量调试后,我发现如果您的连接关闭,您将收到此错误。

因此,如果您遇到此错误,请确保您的连接未关闭。

try {
    connection = getConnection(config.name)
    //after adding this if block, I no longer received this error
    if (!connection.isConnected) { 
        await connection.connect();
    }
} catch(err) {
    connection = await createConnection(config);
}

如果已关闭,请重新连接。

确保使用 @Entity() 注释实体类

例子

@Entity() <-- IMPORTANT
export class Tax {
  @PrimaryGeneratedColumn()
  taxId: number;

  @Column({ type: 'varchar', length: 48 })
  name: string;

  @Column({ type: 'varchar', length: 128 })
  description: string;

  @Column({ type: 'smallint' })
  rate: string;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM