2017-08-31 112 views
0

我试图让下面的示例工作https://github.com/typeorm/javascript-example/tree/master/src/app3-es6RepositoryNotFoundError:Typeorm

我在新RepositoryNotFoundError运行到以下错误

错误 (... \ node_modules \ typeorm \连接\错误\ 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); 
}); 

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

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

module.exports = { 
    Category: Category 
}; 

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

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

我不知道我做错了

回答

1

它看起来像你的实体进口不工作。如果通过通配符导入:

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

确保您的模型已编译为js。您也可以只进口

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