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.

Better error messages when an `@Output` property is not initialized.

See original GitHub issue

Related: #3754 (should be fixed together)

Listing an event in a directive without creating an associated EventEmitter instance results in cryptic error:

TypeError: Cannot read property ‘observer’ of undefined at Function.ObservableWrapper.subscribe (angular2.js:19403) at EventEmitterAccessor.subscribe (angular2.js:26062) at AppViewManagerUtils._setUpEventEmitters (angular2.js:20932) at AppViewManagerUtils._hydrateView (angular2.js:20896) at AppViewManagerUtils.hydrateRootHostView (angular2.js:20814) at AppViewManager.createRootHostView (angular2.js:23990) at angular2.js:13594 at Zone.run (zone.js:113) at Zone.run (angular2.js:12928) at zoneBoundFn (zone.js:86)

To reproduce:

@Directive({
    selector: '[foo]'
})
class Foo {
  @Output() foo; // not initialized to = new EventEmitter();
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:9
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

20reactions
luisfelix199276commented, Nov 20, 2018

For me the error stopped when I changed the import statement from import { EventEmitter } from ‘events’; to import { EventEmitter } from ‘@angular/core’. The first option was suggested by angular as default but I think that the second one should be used. Everything works fine now after I changed it.

Example: @Output() variableToEmit: EventEmitter<string> = new EventEmitter();

showsString(text: any) { this.variableToEmit.emit(text); }

Hope that helps .

2reactions
aldo-romancommented, Jan 16, 2018

Hi @mhevery @vicb I submitted a PR for this issue some time ago. I just fixed a conflict, lint errors and tests. See #19116

Please let me know if there is something I can do. Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

An error occurred: @Output not initialized - angular
The problem: I was initializing the EventEmmitter object in the ngOnInit method from my component class. Solution: I moved the initialization ...
Read more >
How to fix "variable might not have been initialized" error in ...
The error message is very clear, it's saying that local variable "b" has not initialized until line 14, where it has been read,...
Read more >
Input() and @Output() properties - Angular Hispano
An @Output() property should normally be initialized to an Angular EventEmitter with values flowing out of the component as events. Just like with...
Read more >
Writing Functions
The Function node allows JavaScript code to be run against the messages that are passed through it. The message is passed in as...
Read more >
Class Constructor Methods - MATLAB & Simulink
properties · end ; methods function ; %% Pre Initialization %% % Any code not using output argument (obj) ; if · %...
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