Reputation: 834
I am a newbie to the Cloud Functions.
I have an Android app which calls the Cloud Function through callable client and Cloud Function(node.js/typescript) calls Firestore to read the data.
Cloud Function code :
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp({credential : admin.credential.applicationDefault()});
exports.queryForData = functions.https.onCall((dats, context) => {
admin.firestore().collection('mydocument').get()
.then(snapshot => {
snapshot.forEach( e => {
if(e.exists)
{
console.log("docs="+e.ref.path);
return {"data":e.data()};
}
else{
console.log("No docs!!!!!")
return {"data":"empty"}
}
})
})
.catch(error => {
console.log("error occurred"+error);
return {error:error};
});
});
When my Android app calls this function I get an error message caught by catch block shows: ERROR: Could not load the default credential
and asks to check the Google Cloud Service Key set up URL and I have set up the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to the downloaded JSON file.
I have tried initializeApp
with no arguments,with credential with applicationDefault
and also functions.config().firebase
I have put console logs to see if the function is invoked and HTTP call is received, yes it is invoked. But seems like the issue is when I try to get the collection.
Firestore rule allows authenticated users to read the document.
I have gone through the video tutorials and the question asked here before as well but none seems to resolve my issue.
Any help is greatly appreciated.
Upvotes: 0
Views: 329
Reputation: 4465
I initialize using the .json service account file. You can generate this from the Firebase console.
My initialization looksl ike thi
const functions = require('firebase-functions');
const admin = require("firebase-admin");
const serviceAccount = require("./key.json");
const databaseURL = "https://xyz-prod.firebaseio.com";
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL
});
const db = admin.firestore()
const itemsRef = db.collection("items")
Upvotes: 1