1

I'm learning on how tu use Firebase Function inside an Ionic + Angular project.

I want to create my custom function that will get all the games (a collection) and return an array with these games by order of an attribute called "count". My first step is to get all the games and just display to understand the structure.

Unfortunately, when I run my command firebase emulators:start and I access to my function "getMostPlayedGames" this error appears:

FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created call Firebase App.initializeApp() (app/no-app). at app 

And here's my index.ts

import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; import firebase from 'firebase'; admin.initializeApp(); export const helloWorld = functions.https.onRequest((req, res) => { res.send('Hello world'); }); export const deleteExpiredSessions = functions.https.onRequest((req, res) => { functions.logger.info('Automatic cleaning - Delete expired sessions from Database', { structuredData: true }); res.send('Automatic cleaning - Delete expired sessions from database'); }); export const getMostPlayedGames = functions.https.onRequest((req, res) => { const db = firebase.firestore(); const games = db.collection('games'); console.log(games); functions.logger.info('Get all games', { structuredData: true }); res.send(games); });

The "HelloWorld" function works.

1 Answer 1

2

In a Cloud Function, if you want to interact with the Firebase services (e.g. Firestore), you need to use the Admin SDK.

Therefore the following changes (see comments // ...) should do the trick (untested)

import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; // import firebase from 'firebase'; COMMENTED OUT admin.initializeApp(); export const helloWorld = functions.https.onRequest((req, res) => { res.send('Hello world'); }); export const deleteExpiredSessions = functions.https.onRequest((req, res) => { functions.logger.info('Automatic cleaning - Delete expired sessions from Database', { structuredData: true }); res.send('Automatic cleaning - Delete expired sessions from database'); }); export const getMostPlayedGames = functions.https.onRequest((req, res) => { const db = admin.firestore(); // CHANGED const games = db.collection('games'); console.log(games); functions.logger.info('Get all games', { structuredData: true }); res.send(games); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.