Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与数据库的typescript连接_Typescript_Connection_Package_Tsc_Typeorm - Fatal编程技术网

与数据库的typescript连接

与数据库的typescript连接,typescript,connection,package,tsc,typeorm,Typescript,Connection,Package,Tsc,Typeorm,我是typescript新手,我想创建一个与数据库的新连接,并插入我在代码中提到的数据。我正确安装了软件包,我参考了网站typerm.io,无法正确安装typescript/tsc。 src/index.ts import "reflect-metadata"; import {createConnection} from "typeorm"; import {User} from "./entity/User"; createConnection({ type: "mysql",

我是typescript新手,我想创建一个与数据库的新连接,并插入我在代码中提到的数据。我正确安装了软件包,我参考了网站typerm.io,无法正确安装typescript/tsc。 src/index.ts

import "reflect-metadata";
import {createConnection} from "typeorm";
import {User} from "./entity/User";

createConnection({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "welcome123@",
    database: "test",
    entities: [
        User
    ],
    synchronize: true,
    logging: false
}).then(async connection => {

    console.log("Inserting a new user into the database...");
    const user = new User();
    user.firstName = "Timber";
    user.lastName = "Saw";
    user.age = 25;
    await connection.manager.save(user);
    console.log("Saved a new user with id: " + user.id);

    console.log("Loading users from the database...");
    const users = await connection.manager.find(User);
    console.log("Loaded users: ", users);

    console.log("Here you can setup and run express/koa/any other 
   framework.");

}).catch(error => console.log(error));
这是文件,我定义了模型。 实体/用户.ts

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    age: number;

}
不要!!!!!! 这个东西将被转换为纯JS,所有服务器端信息都将被公开(连接信息)。 请尝试连接到API。
构建API并向客户端应用程序公开其URI,并根据需要执行CRUD操作。

代码有什么问题?我无法连接数据库,此代码正确吗?!!是否有错误消息?我应该如何运行此文件、tsc index.ts或节点索引。jsram@thinkpad-t420:~/Desktop/task/ts/src$node index.js/home/ram/node_modules/typeorm/decorator/columns/Column.js:44抛出新ColumnTypeUndefinedError_1.ColumnTypeUndefinedError(对象,属性名称);^ColumnTypeUndefinedError:未定义用户#firstName的列类型,无法猜测。确保在tsconfig.json中打开了“emitDecoratorMetadata”:true选项。还要确保已在应用程序的主条目文件顶部导入“反射元数据”(在导入任何实体之前)