RepositoryNotFoundError : TypeORM

31.9k views Asked by At

I am trying to get the following example working:

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

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/

/*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

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

module.exports = {
    Category: Category
};

PostSchema.js in /entity/

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

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

4

There are 4 answers

3
Buzzzzzzz On

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.

1
Mukarram Ishaq On

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.

2
wenzel On

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. You also could just import

createConnection({ 
    ..., 
    entities: [
        Post,
        ...
    ],}).then(...)
2
Felix K. On

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:

  • ts used when locally running with ts-node
  • js used when having built for production via tsc.

Code:

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