简体   繁体   English

RepositoryNotFoundError : TypeORM

[英]RepositoryNotFoundError : TypeORM

I am trying to get the following example working:我正在尝试使以下示例正常工作:

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

I am running into the following error:我遇到了以下错误:

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?'

here is index.js这是 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);
});

Post.js in /model/ /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
};

Category.js分类.js

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

module.exports = {
    Category: Category
};

PostSchema.js in /entity/ /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
};

CategorySchema.js类别架构.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
};

i dont know what i am doing wrong我不知道我做错了什么

It looks like your entity import is not working.看起来您的实体导入不起作用。 If you import via the wildcard:如果通过通配符导入:

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

Make sure your model is compiled to js.确保您的模型已编译为 js。 You also could just import你也可以只导入

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

For those who are using typescript and experience this problem: Be reminded that you need to include both ts and js file suffixes when specifying the entities-path:对于那些正在使用打字稿并遇到此问题的人:请注意,在指定实体路径时,您需要同时包含tsjs文件后缀:

  • ts used when locally running with ts-node ts使用时与本地运行ts-node
  • js used when having built for production via tsc . js使用时为经由内置的生产tsc

Code:代码:

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

I had the same problem for months and finally figured out what I was doing wrong.几个月来我遇到了同样的问题,终于弄清楚我做错了什么。
When you import Entities, make sure the file names are EXACTLY matching.导入实体时,请确保文件名完全匹配。 It's not going to throw any errors, but during the run time, it's going to throw the above error.它不会抛出任何错误,但在运行时,它会抛出上述错误。
Ex.例如。 In the entity or model classes, if we import like this,在实体或模型类中,如果我们这样导入,

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

it's different from它不同于

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

It won't show any errors, but when you try to call the table, it will show the exact same error.它不会显示任何错误,但是当您尝试调用该表时,它会显示完全相同的错误。

I had the same problem.我有同样的问题。 None of the solutions worked for me.没有一个解决方案对我有用。 After much debugging I figured out that you'll receive this error if your connection is closed.经过大量调试后,我发现如果您的连接关闭,您将收到此错误。

So if you are facing this error, make sure your connection is not closed.因此,如果您遇到此错误,请确保您的连接未关闭。

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);
}

If it is closed, connect it again.如果已关闭,请重新连接。

Make sure you annotated the entity class with @Entity()确保使用 @Entity() 注释实体类

Example例子

@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