RepositoryNotFoundError:Typeorm

Sho*_*nda 3 javascript node.js typeorm

我正在尝试让以下示例工作https://github.com/typeorm/javascript-example/tree/master/src/app3-es6

我遇到以下错误

Connection.findRepositoryAggregator(... \ node_modules \ typeorm \ connection \ Connection.js:513:19)处的新RepositoryNotFoundError(... \ node_modules \ typeorm \ connection \ error \ RepositoryNotFoundError.js:24:23)处的错误.getRepository(... \ node_modules \ typeorm \ connection \ Connection.js:405:21)位于... \ index.js:27:37

name: 'RepositoryNotFoundError',
  message: 'No repository for "Post" was found. Looks like this entity is not registered in current "default" connection?'
Run Code Online (Sandbox Code Playgroud)

这是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);
});
Run Code Online (Sandbox Code Playgroud)

/ 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
};
Run Code Online (Sandbox Code Playgroud)

Category.js

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

module.exports = {
    Category: Category
};
Run Code Online (Sandbox Code Playgroud)

/ 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
};
Run Code Online (Sandbox Code Playgroud)

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
};
Run Code Online (Sandbox Code Playgroud)

我不知道我在做什么错

B12*_*ter 9

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

  • ts 在本地运行时使用 ts-node
  • js在通过tsc.

代码:

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

  • 这是一个很好的答案,值得添加到官方文档中。 (3认同)
  • 答案是正确的!我的 NestJs 项目也遇到了类似的问题 (2认同)

wen*_*zel 6

看来您的实体导入无效。如果通过通配符导入:

entities: [
    __dirname + "/entity/*.js"
],`
Run Code Online (Sandbox Code Playgroud)

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

createConnection({ 
    ..., 
    entities: [
        Post,
        ...
    ],}).then(...)
Run Code Online (Sandbox Code Playgroud)

  • 2 年后,当我尝试向 createConnection 添加配置选项时,它不起作用...... (2认同)

Buz*_*zzz 5

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

import { FooClass } from "./foo-Class.model";
Run Code Online (Sandbox Code Playgroud)

它不同于

import { FooClass } from "./foo-class.model";
Run Code Online (Sandbox Code Playgroud)

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