angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Universal @ Firebase Cloud Functions: TypeError: ctor is not a constructor

Open SpeedoPasanen opened this issue 9 years ago • 8 comments

Version info

Angular: 4.3.5

Firebase: 4.3.0

AngularFire: 4.0.0-rc.2

Other (e.g. Ionic/Cordova, Node, browser, operating system): Node 8.x locally, 6.x on Cloud Functions.

How to reproduce these conditions

https://gist.github.com/funkizer/10d34128f126549bda2af7a650f4dcf9 Followed these examples: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/universal-rendering.md https://medium.com/@cdeniz/angular-universal-on-firebase-dynamic-hosting-4fdd034af3db

Steps to set up and reproduce

Had a complete working universal app, which renders without errors when run locally with firebase serve --only functions,hosting or in my own Express server. When deployed to Firebase, get this error at function run time.

Replaced AppModule with a MockAppModule that bootstraps only a simple hello world -component.

Started removing imports one at a time, building and deploying each time again, found out that this error happens when either AngularFireDatabaseModule or AngularFireAuthModule, or both are imported. Commenting those two out and leaving everything else makes it work.

Debug output

** Errors in Firebase Cloud Functions Log **

TypeError: ctor is not a constructor at createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:9544:26) at createProviderInstance$1 (/user_code/node_modules/@angular/core/bundles/core.umd.js:9519:26) at initNgModule (/user_code/node_modules/@angular/core/bundles/core.umd.js:9473:13) at new NgModuleRef (/user_code/node_modules/@angular/core/bundles/core.umd.js:10587:9) at Object.createNgModuleRef (/user_code/node_modules/@angular/core/bundles/core.umd.js:10571:12) at NgModuleFactory.create (/user_code/node_modules/@angular/core/bundles/core.umd.js:13860:25) at /user_code/node_modules/@angular/core/bundles/core.umd.js:4529:61 at ZoneDelegate.invoke (/user_code/node_modules/zone.js/dist/zone-node.js:391:26) at Object.onInvoke (/user_code/node_modules/@angular/core/bundles/core.umd.js:3922:33) at ZoneDelegate.invoke (/user_code/node_modules/zone.js/dist/zone-node.js:390:32)

Expected behavior

Should behave the same way when deployed and when served locally with firebase serve --only functions.

Actual behavior

Crashes and timeouts when deployed cloud function is executed, no problems running locally, gets initial data from FB Database etc.

SpeedoPasanen avatar Aug 21 '17 15:08 SpeedoPasanen

I'll see if I can get AngularfireDatabase and *Auth working by the approach in https://github.com/angular/universal-starter instead of the two tutorials I used and linked above, and update around next weekend.

Starter has some differences, like no Angular-cli used, just Webpack, everything in TS compiled to JS, And for some reason I couldn't get Functions to even deploy (some reflector error) without requiring 'angular2-universal-polyfills/node', which I guess is not supposed to be used anymore? Starter doesn't use that either.

If this works I'll rewrite my app around the starter and recommend anyone looking into using Angularfire2 with Universal and Cloud Functions do the same.

SpeedoPasanen avatar Aug 23 '17 06:08 SpeedoPasanen

It works! Using https://github.com/angular/universal-starter as base I don't need to worry about importing Angularfire2 modules. Obviously can't use Auth serverside and need to stub that, but Database doesn't work either. When I subscribe to a FirebaseObjectObservable, I get a timeout sometimes, 504 gateway error sometimes. Is it WIP?

SpeedoPasanen avatar Aug 24 '17 17:08 SpeedoPasanen

Solved. I went back to Angular-cli because I just couldn't get webpack to watch-build in less than 10 seconds. That's painful.

Got everything working by:

  • For server side, provide a mock AuthService and a DatabaseService that uses firebase-admin passed as an extraProvider to the NgModuleFactory from the functions JS side to Angular
  • In ServerDatabase, set a 10 second timeout to make Universal wait for data, wrap admin.database().ref() -calls in zone.runOutsideAngular(), and in .then(), clear the timeout wrapped inside zone.run() so that Universal stops waiting. If you use admin.database().ref inside Angular's zone, function hangs and timeouts after 60 seconds, giving no error. Tried database().goOffline, app.delete(), etc. to no avail. zone.runOutsideAngular was the key.
  • Make sure FirebaseListObservable or *ObjectObservable aren't imported in any service anywhere (or anything that gets injected), otherwise that injection fails with gloriously untraceable errors. Instead, make DatabaseService return an Observable that's spiced with mock push, set, update etc. functions that those Angularfire2 classes have. It's ok to import FirebaseListObservable and Object anywhere else in the app, like in components.

Too busy and exhausted by this ordeal to post a stripped down example, but I'll be happy to help if you face similar problems. :)

Finally, some seo x) https://diachats.com

SpeedoPasanen avatar Sep 01 '17 19:09 SpeedoPasanen

@funkizer - can you please share your code as a reference

sonirahul avatar Oct 07 '17 19:10 sonirahul

@funkizer I really have exactly same problem when I using angular universal with firebase

It alwasy goes to as I start import AngularFireDatabase from 'angularfire2/database'

Could you share some codes of your solution?

ruucm-working avatar Oct 19 '17 21:10 ruucm-working

I am facing the same problem! However, I am note sure if the crash comes directly from angularfire2's module.....

AnthonyNahas avatar Jan 31 '18 10:01 AnthonyNahas

I faced this issue as well, never found the root cause. Everything works locally but does not work when deployed as a firebase cloud function

FrozenPandaz avatar Feb 02 '18 04:02 FrozenPandaz

I fixed the problem by following the steps mentioned here: https://stackoverflow.com/questions/44597532/angular-4-unit-testing-error-typeerror-ctor-is-not-a-constructor/48753465#48753465

I do feel I got lucky though, as I never found out what package actually caused the error. Just removing the node_modules and updating to all the latest version, then installing them did the trick.

cmddavid avatar Feb 11 '18 21:02 cmddavid