Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instance[output.propName].subscribe is not a function resulting from polyfill

I'm running into a strange issue. I've researched the error, and it generally seems to be related to the use of @Output or EventEmitter, but that's not the case here.

I get the following error in my application if I have a particular polyfill in place:

TypeError: instance[output.propName].subscribe is not a function
    at createDirectiveInstance (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:12319:71)
    at createViewNodes (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:13776:38)
    at callViewAction (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:14210:1)
    at execComponentViewsAction (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:14119:1)
    at createViewNodes (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:13804:1)
    at createRootView (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:13665:1)
    at callWithDebugContext (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:15090:26)
    at Object.debugCreateRootView [as createRootView] (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:14373:1)
    at ComponentFactory_.webpackJsonp.../../../core/esm5/core.js.ComponentFactory_.create (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:11260:26)
    at initComponent (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/testing.js:1137:1)

The polyfill is as follows:

Object.prototype.clone = function(deep = false) {
    return (deep)
        ? JSON.parse(JSON.stringify(this)) // Deep clone - copies object property values in full.
        : Object.assign({}, this);         // Shallow clone - copies property values (including object references rather than object).
};

Can anyone help me understand why this might cause an error inside createDirectiveInstance? If I comment this code out, the error goes away. It's not simply having a polyfill that causes problems - there's another, which adds Array.prototype.upsert, which doesn't cause problems.

like image 942
Stuart Updegrave Avatar asked Feb 03 '18 11:02

Stuart Updegrave


1 Answers

I have just faced this issue and checking the code I wrote I discovered that it was caused due to a bad import of the EventEmitter class (done by the autoimport of visual studio code).

You just need to change

import { EventEmitter } from 'events';

To

import { EventEmitter } from '@angular/core';

And about the error explanation. Angular maps the events as observables, so it has to subscribe to the children components' events and send them to the appropiate components.

Looking for the original package I found its source code on: https://github.com/Gozala/events/blob/master/events.js

And it simply fails because there isn't a method .suscribe() in the EventEmitter you have created.

like image 131
Adrian Abreu Avatar answered Oct 13 '22 21:10

Adrian Abreu