Universal @ Firebase Cloud Functions: TypeError: ctor is not a constructor
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.
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.
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?
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
@funkizer - can you please share your code as a reference
@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?
I am facing the same problem! However, I am note sure if the crash comes directly from angularfire2's module.....
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
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.