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.

TypeError: Cannot read property 'getContext' of undefined

See original GitHub issue

Hello am trying to retrieve one record from the database am getting this error “TypeError: Cannot read property ‘getContext’ of undefined”. What could be the issue?

Below is my code in an entity repository that’s being called in a service in Nest Js Framework

async validateUserPassword(signInCredentialsDto: SignInCredentialsDto): Promise<LoginUsers | null> { const { email, password } = signInCredentialsDto; const user = await this.em.findOne(LoginUsers, { email: email }) console.log(user) if(user && await user.validatePassword(password)){ return user; }else{ return null; } }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
EduVencovskycommented, Sep 22, 2021

Here is an example on how to get this error.

If you have a module of an entity Foo and also have a repository for Foo, you would have something like this

@Entity()
export class Foo {
  [EntityRepositoryType]?: FooRepository

  // other fields ...
}
@Repository(Foo)
export class FooRepository extends EntityRepository<Foo> {}

And in the module you would need to use MikroOrmModule.forFeature to register the entity and repository (using EntityRepositoryType in the entity), which would look something like:

@Module({
  imports: [MikroOrmModule.forFeature({ entities: [Foo] })],
  providers: [ /* foo services or providers */ ],
  controllers: [ /* foo controllers */ ]
})
export class FooModule {}

Later you think something like “I want to use this repository in other module, so I will export it” and you will do:

@Module({
  imports: [MikroOrmModule.forFeature({ entities: [Foo] })],
  providers: [ /* foo services or providers */, FooRepository], // repository added to providers
  exports: [FooRepository],
  controllers: [ /* foo controllers */ ]
})
export class FooModule {}

and in another module you would import FooModule and add to the constructor of your service FooRepository.

@Module({
  imports: [FooModule, /* bar module imports*/ ],
  providers: [ /* bar services or providers */ ], 
  controllers: [ /* bar controllers */ ]
})
export class BarModule {}
@Injectable()
export class BarService {
  constructor(public readonly fooRepository: FooRepository, /* other things injected*/) {}
}

And this is what will cause the error when trying to use the repositories in difference places.
I’m not sure if this is intentional or if it’s a bug/something not possible to do (Maybe @B4nan could say something about it).

To solve this you need to remove the FooRepository from providers and exports from the FooModule and to use it in the BarModule you must use MikroOrmModule.forFeature to register Foo entity and it’s repository.

@Module({
  imports: [ 
    MikroOrmModule.forFeature({
      entities: [FooEntity, BarEntity, /* other entities */],
    }),
  ],
  providers: [ /* bar services or providers */ ], 
  controllers: [ /* bar controllers */ ]
})
export class BarModule {}

This worked for me and hope this comment is good for people in the future.

7reactions
FelipeEmerimcommented, Apr 16, 2021

I had the same problem, in my case I was declaring my custom repository as a provider and also importing it in the module configuration. Removing the repository from the providers array solved it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'getContext' of null, using canvas
I get the error Uncaught TypeError: Cannot read property 'getContext' of null and the important parts in files are... I am wondering since...
Read more >
TypeError: Cannot read property 'getContext' of Null in JS
The "Cannot read property 'getContext' of null" error occurs when calling the getContext method on a null value. To solve the error, make...
Read more >
Uncaught typeerror cannot read property 'GETCONTEXT' of null
The video shows how to correct the error when drawing on a canvas html5 "Uncaught typeerror cannot read property 'GETCONTEXT' of ...
Read more >
Why is my code not going through? it's indicating ... - Sololearn
it's indicating >>> uncaught TypeError! cannot read property 'getContext' of null line: 5. var canvas= document.getElementById("canvas"); var contex= canvas.
Read more >
Cannot read properties of null (reading 'getContext') - GSAP
Hi, I'm getting console errors "Cannot read properties of null (reading 'getContext')" and I'm not sure how to correct. My code is below....
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