question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createConnection TypeError: Cannot read property 'type' of undefined

See original GitHub issue

Issue type:

[x ] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [x ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

export class Media {
  @ObjectIdColumn()
  public id: ObjectID;

  @Column()
  public instagramId: string;

  @Column({ enum: ['image', 'video']})
  public type: 'image' | 'video';

  @OneToOne(type => InstaLink, link => link.media)
  public link: InstaLink;

  @Column()
  public caption: string;

  @Column()
  public instagramCreatedTime: number;

  @Column()
  public images: {
    thumbnail?: IMediaImage;
    lowResolution?: IMediaImage;
    standardResolution?: IMediaImage;
  };

  @ManyToOne(type => User, user => user.media)
  public user: User;

  constructor(instagramMedia: InstagramMedia, user: User) {
    this.type = instagramMedia.type;
    this.instagramId = instagramMedia.id;
    this.caption = instagramMedia.caption.text;
    this.images = {
      thumbnail: {
        url: instagramMedia.images.thumbnail.url,
        height: instagramMedia.images.thumbnail.height,
        width: instagramMedia.images.thumbnail.width
      },
      lowResolution: {
        url: instagramMedia.images.low_resolution.url,
        height: instagramMedia.images.low_resolution.height,
        width: instagramMedia.images.low_resolution.width
      },
      standardResolution: {
        url: instagramMedia.images.standard_resolution.url,
        height: instagramMedia.images.standard_resolution.height,
        width: instagramMedia.images.standard_resolution.width
      }
    };
    this.user = user;
    this.instagramCreatedTime = parseInt(instagramMedia.created_time);
  }
}

I have this entity and when I’m creating connection i get this error :

TypeError: Cannot read property 'type' of undefined
    at new Media (/home/vrondelli/projects/tokilabs/livebio/service/src/models/media.ts:39:32)
    at EntityMetadata.create (/home/vrondelli/projects/tokilabs/livebio/service/src/metadata/EntityMetadata.ts:499:19)
    at EntityMetadataValidator.validate (/home/vrondelli/projects/tokilabs/livebio/service/src/metadata-builder/EntityMetadataValidator.ts:111:47)
    at /home/vrondelli/projects/tokilabs/livebio/service/src/metadata-builder/EntityMetadataValidator.ts:44:56
    at Array.forEach (<anonymous>)
    at EntityMetadataValidator.validateMany (/home/vrondelli/projects/tokilabs/livebio/service/src/metadata-builder/EntityMetadataValidator.ts:44:25)
    at Connection.buildMetadatas (/home/vrondelli/projects/tokilabs/livebio/service/src/connection/Connection.ts:505:33)
    at Connection.<anonymous> (/home/vrondelli/projects/tokilabs/livebio/service/src/connection/Connection.ts:189:18)
    at step (/home/vrondelli/projects/tokilabs/livebio/service/node_modules/typeorm/node_modules/tslib/tslib.js:133:27)
    at Object.next (/home/vrondelli/projects/tokilabs/livebio/service/node_modules/typeorm/node_modules/tslib/tslib.js:114:57)
    at fulfilled (/home/vrondelli/projects/tokilabs/livebio/service/node_modules/typeorm/node_modules/tslib/tslib.js:104:62)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)
    at emitWarning (internal/process/promises.js:65:17)
    at emitPendingUnhandledRejections (internal/process/promises.js:109:11)
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
ghostcommented, Apr 12, 2019

Well, I got your error it says you are trying to use type of undefined which probably throwing from that this.type = instagramMedia.type;

NOTE: Please do not post issues outside of typeorm’s scope.

1reaction
vlapocommented, Apr 13, 2019

Hey @vrondelli. From docs https://typeorm.io/#/entities.

When using an entity constructor its arguments must be optional. Since ORM creates instances of entity classes when loading from the database, therefore it is not aware of your constructor arguments.

So mark your constructor arguments as optional and handle undefined values.

constructor(instagramMedia?: InstagramMedia, user?: User) {

Warning: This behavior is subject to change in next versions as it is discussed in https://github.com/typeorm/typeorm/issues/1772, https://github.com/typeorm/typeorm/pull/3845 and also https://github.com/typeorm/typeorm/pull/3910.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - TypeError: Cannot read property 'createConnection ...
I'm new to node, and I'm trying to create a zendesk app that connects to mysql when the user inputs a value. Here's...
Read more >
TypeError: Cannot read properties of undefined ... - MongoDB
I have this code here: import { MongoClient } from "mongodb"; const createConnection = () => { // let current = null; const...
Read more >
How to resolve a 'cannot read property connect of undefined ...
You get this error because the variable object is not defined, and trying to retrieve, set or mutate the property “id” on undefined...
Read more >
Node JS TypeError: Cannot read property 'query' of undefined
I will provide a simple example of finding an error cannot read property 'query' of undefined. In this example use CRUD in the...
Read more >
cannot read properties of undefined (reading 'query') - You.com
The app.use('/api/addReferFriend', addReferFriend); has to be after the app.use(async function(req, res, next) { .. let connection = mysql.createConnection( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found