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);