Skip to content

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

Closed

Description

@pkozlowski-opensource

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();
}

Activity

  1. added a commit that references this issue on Aug 17, 2015
    574f865
  2. mhevery commented on Jul 1, 2016

    @mhevery
    Contributor

    Is this still an issue? Can you fix it?

  3. 11 remaining items

  4. ashish173 commented on Jan 19, 2017

    @ashish173

    Yes I also got this cryptic error message. I am going to try to fix this one.

  5. cedvdb commented on Apr 16, 2017

    @cedvdb

    When subscribing to the non initialized EventEmitter in the template eg:

        <editor (submitted)="onReply()"></editor>
       // with 
          @Output() submitted;
    

    The error message is also not very precise as of where the error is coming from.

    core.es5.js:1084 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'subscribe' of undefined
    TypeError: Cannot read property 'subscribe' of undefined
    
  6. aldo-roman commented on Jan 16, 2018

    @aldo-roman
    Contributor

    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!

  7. added this to the Backlog milestone on Jan 23, 2018
  8. splincode commented on Jul 26, 2018

    @splincode
    Contributor

    @pkozlowski-opensource Is the issue relevant?
    Will this pull request be accepted?
    #19116

  9. added a commit that references this issue on Aug 2, 2018
    35f0135
  10. luisfelix199276 commented on Nov 20, 2018

    @luisfelix199276

    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 = new EventEmitter();

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

    Hope that helps .

  11. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  12. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions