firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

app.database is not a function at new AngularFireDatabase only on server (ssr function)

Open lcoppinger opened this issue 8 years ago • 6 comments

Version info

firebase-functions: 1.0.2

firebase-tools: 3.18.4

firebase-admin: 5.12.1

angularfire2 5.0.0-rc.7

Steps to reproduce

Running firebase serve renders both browser and server side content from firebase realtime database using Angularfire2

Running firebase deploy deploys code successfully and ssr works for all content not requested by angularfire2 database. However on page load firebase logs show error (see below).

package.json

{ "name": "functions", "scripts": { "serve": "firebase serve --only functions", "shell": "firebase experimental:functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions", "logs": "firebase functions:log" }, "dependencies": { "@amcharts/amcharts3-angular": "./es5_modules/@amcharts/amcharts3-angular", "@angular/animations": "5.0.0-rc.5", "@angular/cdk": "^5.2.5", "@angular/cli": "^1.6.8", "@angular/common": "5.0.0-rc.5", "@angular/compiler": "5.0.0", "@angular/core": "5.0.0-rc.5", "@angular/flex-layout": "^5.0.0-beta.14", "@angular/forms": "5.0.0-rc.5", "@angular/http": "5.0.0-rc.5", "@angular/platform-browser": "^5.0.0-rc.0", "@angular/platform-browser-dynamic": "5.0.0-rc.5", "@angular/platform-server": "^5.2.10", "@angular/router": "5.0.0-rc.5", "@firebase/app": "^0.3.2", "@firebase/database": "^0.3.2", "@fortawesome/fontawesome-svg-core": "^1.2.0-11", "@fortawesome/free-brands-svg-icons": "^5.1.0-8", "@fortawesome/free-solid-svg-icons": "^5.1.0-8", "@nguniversal/express-engine": "^6.0.0", "@nguniversal/module-map-ngfactory-loader": "^5.0.0", "@ngx-share/core": "^5.0.1", "@types/angular-google-analytics": "^1.1.33", "@types/d3": "^5.0.0", "@types/express": "^4.11.1", "@types/google.analytics": "0.0.36", "angular-universal-express-firebase": "0.0.4", "angularfire2": "^5.0.0-rc.7.2-next", "babel-cli": "^6.26.0", "babel-preset-es2015": "^6.24.1", "bufferutil": "^3.0.4", "core-js": "^2.4.1", "d3": "^4.9.1", "d3-geo": "^1.10.0", "d3-queue": "^3.0.7", "express": "^4.16.3", "firebase": "^4.13.1", "firebase-admin": "^5.12.1", "firebase-functions": "^1.0.2", "font-awesome": "^4.7.0", "jquery": "^3.2.1", "ngx-google-places-autocomplete": "./es5_modules/ngx-google-places-autocomplete", "ngx-slick": "^0.1.3", "reflect-metadata": "^0.1.12", "rxjs": "^5.5.10", "topojson": "^3.0.0", "typescript": "^2.4.2", "utf-8-validate": "^4.0.1", "web-animations-js": "^2.3.1", "ws": "^5.1.1", "xmlhttprequest": "^1.8.0", "zone.js": "^0.8.14" }, "devDependencies": { "typescript": "^2.5.3" }, "private": true }

Were you able to successfully deploy your functions?

Yes.

Expected behavior

Firebase ssr function renders angularfire2 response data from the server as well as the browser as it does locally.

Actual behavior

On local everything works fine, on remote server when page making a call through AngularfireDatabase:

ERROR { Error: Uncaught (in promise): TypeError: app.database is not a function TypeError: app.database is not a function at new AngularFireDatabase (/user_code/node_modules/angularfire2/bundles/database.umd.js:263:29) at _createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:10670:20) at _createProviderInstance$1 (/user_code/node_modules/@angular/core/bundles/core.umd.js:10634:26) at resolveNgModuleDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:10619:17) at _createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:10660:29) at _createProviderInstance$1 (/user_code/node_modules/@angular/core/bundles/core.umd.js:10634:26) at resolveNgModuleDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:10619:17) at NgModuleRef_.get (/user_code/node_modules/@angular/core/bundles/core.umd.js:11844:16) at resolveDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:12340:45) at createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:12210:32) at resolvePromise (/user_code/node_modules/zone.js/dist/zone-node.js:814:31) at resolvePromise (/user_code/node_modules/zone.js/dist/zone-node.js:771:17) at /user_code/node_modules/zone.js/dist/zone-node.js:873:17 at ZoneDelegate.invokeTask (/user_code/node_modules/zone.js/dist/zone-node.js:421:31) at Object.onInvokeTask (/user_code/node_modules/@angular/core/bundles/core.umd.js:4656:33) at ZoneDelegate.invokeTask (/user_code/node_modules/zone.js/dist/zone-node.js:420:36) at Zone.runTask (/user_code/node_modules/zone.js/dist/zone-node.js:188:47) at drainMicroTaskQueue (/user_code/node_modules/zone.js/dist/zone-node.js:595:35) at ZoneTask.invokeTask (/user_code/node_modules/zone.js/dist/zone-node.js:500:21) at ZoneTask.invoke (/user_code/node_modules/zone.js/dist/zone-node.js:485:48) rejection: TypeError: app.database is not a function at new AngularFireDatabase (/user_code/node_modules/angularfire2/bundles/database.umd.js:263:29) at _createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:10670:20) at _createProviderInstance$1 (/user_code/node_modules/@angular/core/bundles/core.umd.js:10634:26) at resolveNgModuleDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:10619:17) at _createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:10660:29) at _createProviderInstance$1 (/user_code/node_modules/@angular/core/bundles/core.umd.js:10634:26) at resolveNgModuleDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:10619:17) at NgModuleRef_.get (/user_code/node_modules/@angular/core/bundles/core.umd.js:11844:16) at resolveDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:12340:45) at createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:12210:32), promise: ZoneAwarePromise { __zone_symbol__state: 0, __zone_symbol__value: TypeError: app.database is not a function at new AngularFireDatabase (/user_code/node_modules/angularfire2/bundles/database.umd.js:263:29) at _createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:10670:20) at _createProviderInstance$1 (/user_code/node_modules/@angular/core/bundles/core.umd.js:10634:26) at resolveNgModuleDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:10619:17) at _createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:10660:29) at _createProviderInstance$1 (/user_code/node_modules/@angular/core/bundles/core.umd.js:10634:26) at resolveNgModuleDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:10619:17) at NgModuleRef_.get (/user_code/node_modules/@angular/core/bundles/core.umd.js:11844:16) at resolveDep (/user_code/node_modules/@angular/core/bundles/core.umd.js:12340:45) at createClass (/user_code/node_modules/@angular/core/bundles/core.umd.js:12210:32) }, zone: Zone { _properties: { isAngularZone: true }, _parent: Zone { _properties: {}, _parent: null, _name: '<root>', _zoneDelegate: [Object] }, _name: 'angular', _zoneDelegate: ZoneDelegate { _taskCounts: [Object], zone: [Circular], _parentDelegate: [Object], _forkZS: null, _forkDlgt: null, _forkCurrZone: [Object], _interceptZS: null, _interceptDlgt: null, _interceptCurrZone: [Object], _invokeZS: [Object], _invokeDlgt: [Object], _invokeCurrZone: [Circular], _handleErrorZS: [Object], _handleErrorDlgt: [Object], _handleErrorCurrZone: [Circular], _scheduleTaskZS: [Object], _scheduleTaskDlgt: [Object], _scheduleTaskCurrZone: [Circular], _invokeTaskZS: [Object], _invokeTaskDlgt: [Object],

Its almost as if the firebase config for the app is not initialising on the server - is this something that needs to go inside my express engine?

lcoppinger avatar May 18 '18 15:05 lcoppinger

Sorry, I'm having trouble understanding this. Why do you believe this is a functions issue? What is SSR? And by "server side content" do you mean you're using a hosting functions proxy integration?

laurenzlong avatar May 18 '18 18:05 laurenzlong

Sorry I will try and make it a bit clearer. I have an Angular Universal app which I want to host on firebase hosting. I have wrapped my express engine in a firebase function called SSR which does successfully render the angular code from the server.

However when I render the page that loads data from my firebase database, using Angularfire2 I get the error on the console: TypeError: app.database is not a function at new AngularFireDatabase and the html for the database data does not render.

I can't work out whats causing it at all, I think its a problem with Angularfire initialising on the server, but I'm not sure if its due to an Angularfire issue or with firebase functions.

lcoppinger avatar May 18 '18 19:05 lcoppinger

This is my server.ts:

`// These are important and needed before anything else import 'zone.js/dist/zone-node'; import 'reflect-metadata';

import { enableProdMode } from '@angular/core'; import * as express from 'express'; import { join } from 'path';

// NOTE: leave this as require() since this file is built Dynamically from webpack const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./server/main.bundle');

// NgUniversalTools: Express Engine and moduleMap for lazy loading import { ngExpressEngine } from '@nguniversal/express-engine'; import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';

//firebase cloud functions import * as firebaseFunctions from 'firebase-functions';

// Faster server renders w/ Prod mode (dev mode never needed) enableProdMode();

//check if Firebase functions is enabled or not const DISABLE_FIREBASE = process.env.DISABLE_FIREBASE || false;

// Express server const app = express(); const PORT = process.env.PORT || 4000; const DIST_FOLDER = join(process.cwd(), DISABLE_FIREBASE ? 'dist' : './');

app.engine('html', ngExpressEngine({ bootstrap: AppServerModuleNgFactory, providers: [ provideModuleMap(LAZY_MODULE_MAP) ] }));

app.set('view engine', 'html'); app.set('views', join(DIST_FOLDER, 'browser'));

/* TODO: implement data requests securely // app.get('/api/*', (req, res) => { // res.status(404).send('data requests are not supported'); // }); */

// All regular routes use the Universal engine app.get('*', (req, res) => { res.render(join(DIST_FOLDER, 'browser', 'index-1.html'), {req}); });

if(DISABLE_FIREBASE){ // Server static files from express in case there's no firebase hosting app.get('.', express.static(join(DIST_FOLDER, 'browser')));

// Start up the Node server if not using firebase cloud functions
app.listen(PORT, () => {
    console.log(`Node server listening on http://localhost:${PORT}`);
});

}

//server side rendering using frebase cloud functions export let ssr = DISABLE_FIREBASE ? null : firebaseFunctions.https.onRequest(app);`

lcoppinger avatar May 18 '18 19:05 lcoppinger

I'm trying to look into firebase-admin to initialise the app, but I'm not sure if that is the answer

lcoppinger avatar May 18 '18 19:05 lcoppinger

Thanks for the clarification. I'm not super familiar with AngularFire, so I'm not sure if this is an appropriate use of AngularFire. I think the best thing to do is to post a StackOverflow question and see if anybody else has tried to use AngularFire inside of a function before. I'm going to close this issue since it's a usage question, which is best for StackOverflow.

laurenzlong avatar May 21 '18 18:05 laurenzlong

I am facing the same issue. Please try using the latest version of AngularFire2

kunalganglani avatar Jul 27 '18 16:07 kunalganglani