typeorm: Cannot read property 'name' of undefined

Hi,

I’m using postgres as my backend and I have a simple entity that I am trying to create. To keep it simple I have removed all of my relationships.

import {Table, PrimaryColumn, Column} from 'typeorm';

@Table("USER")
export class UserDB {

    @PrimaryColumn('int', { generated: true })
    id: number;

    @Column('string', {name: 'USERNAME', length: '50'})
    username: string;

    @Column('string', {name: 'PASSWORD', length: '50'})
    password: string;

    @Column('string', {name: 'LAST_NAME', length: '50'})
    lastName: string;

    @Column('string', {name: 'FIRST_NAME', length: '50'})
    firstName: string;

    @Column('string', {name: 'EMAIL', length: '250'})
    email: string;

    @Column('string', {name: 'PHONE_NUMBER', length: '20'})
    phoneNumber: string;

}

I seem to be connecting to the server OK using this code:

let dbHost = process.env.DB_HOST || 'localhost'
        let dbPort = process.env.DB_PORT || 5432;
        let dbName = process.env.DB_NAME || 'TEST_DB';
        let dbUserName = process.env.DB_PASS || 'testuser';
        let dbPassword = process.env.DB_USER || 'testpw';

        const options: ConnectionOptions = {
            driver: {
                type: 'postgres', // specify driver type here.
                host: dbHost,
                port: dbPort,
                username: dbUserName,
                password: dbPassword,
                database: dbName
            },
            autoSchemaCreate: true,
            entities: [UserDB],
            logging: {
                logQueries: true,
                logSchemaCreation: true
            }
            // entityDirectories: ['./server/model/**/{*.ts,*.js}'],
            // subscriberDirectories: ['./server/subscriber/**/{*.ts,*.js}']
        };

        createConnection(options).then(connection => {

            // at this point you are connected to the database, and you can
            // perform queries

        }).catch(error => console.log('Error during connection to the db: ', error));

After running this I receive the following error.

/Users/jt/Documents/workspace/tester/node_modules/typeorm/metadata/types/ColumnTypes.js:64
        return type.name.toLowerCase();
                   ^

TypeError: Cannot read property 'name' of undefined
    at Function.typeToString (/Users/jt/Documents/workspace/tester/node_modules/typeorm/metadata/types/ColumnTypes.js:64:20)
    at /Users/jt/Documents/workspace/tester/node_modules/typeorm/decorator/columns/PrimaryColumn.js:20:57
    at DecoratePropertyWithoutDescriptor (/Users/jt/Documents/workspace/tester/node_modules/reflect-metadata/Reflect.js:555:13)
    at Object.decorate (/Users/jt/Documents/workspace/tester/node_modules/reflect-metadata/Reflect.js:108:20)
    at __decorate (/Users/jt/Documents/workspace/tester/src/server/model/user.db.js:4:92)
    at /Users/jt/Documents/workspace/tester/src/server/model/user.db.js:12:5
    at Object.<anonymous> (/Users/jt/Documents/workspace/tester/src/server/model/user.db.js:37:2)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
events.js:160
      throw er; // Unhandled 'error' event
      ^

Is this an issue or have I configured things incorrectly? Is there a workaround?

Many thanks

JT

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 30 (16 by maintainers)

Commits related to this issue

Most upvoted comments

you need to add following lines in your tsconfig.json:

        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,

OK, that worked.

I am guessing that this may be some sort of problem with a library conflict or something.

I will try to refactor the project structure. I’ll come back when that’s done.