1
votes

This code does not return any data in localhost, but works well when deployed in firebase. Any suggestions? (tried with both 'firebase emulators:start' & 'npm run serve'). I should get a json object containing courses.
I tried with try/catch without any result:

  • 'npm run server' ("npm run build && firebase serve --only functions")

Error: Could not load the default credentials. Error: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()

  • 'firebase emulators:start' -> no error, but an empty json: {"courses":[]}

Link to code: https://github.com/angular-university/firebase-course/blob/1-firebase-finished/functions/src/index.ts

  import * as functions from 'firebase-functions';
    import {db} from './init';
    import * as express from "express";
    const cors = require('cors');
    const app = express();
    app.use(cors({origin:true}));

    app.get('/courses', async (request, response) => {
        const snaps = await db.collection('courses').get();
        const courses:any[] = [];
        snaps.forEach(snap => courses.push(snap.data()));
        response.status(200).json({courses});
    });

    export const getCourses = functions.https.onRequest(app);

_____________________________________________________________________________

    init.ts

    const admin = require('firebase-admin');
    admin.initializeApp();
    export const db = admin.firestore();

app.get('/courses', async (request, response) => {
    try {
        const snaps = await db.collection('courses').get();
        const courses:any[] = [];
        snaps.forEach((snap: any) => courses.push(snap.data()));
        response.status(200).json({courses});
    } 
    catch(err) {
        console.error(err) 
    }
});
export const getCourses = functions.https.onRequest(app);

1
What exactly are you doing and observing that's different than what you expect? Please edit the question and be specific, so that anyone can reproduce your experience.Doug Stevenson
Sure, I put some more info. Thanks for the suggestion.shaebi
The error message says "This error originated either by throwing inside of an async function without a catch block". Do you need a try/catch to find out if the database operation had some failure?Doug Stevenson
I have tried with try/catch without any result. Is there another way to do try/catch?shaebi

1 Answers

0
votes

You need to provide the rights credentials. It works on google cloud because there are environment variables that make this automatically.


You need to set locally this environment variable with the path to your credentials file. Try this in your terminal:

export FIREBASE_CONFIG="/user/path/to/credential.json"

More info


Try following the documentation. It gives good examples on how to do this.