Reputation: 383
I am quiet new to ionic and cloud function and I have tried to implement a cloud function with some reading but I am not sure if I use it correctly.. I would like to retrieve some collections of my db in firebase.
I have a cloud function in firestore to retrieve the collection of my db in my ionic4 app :
const admin = require('firebase-admin');
const cors = require('cors')({ origin: true });
admin.initializeApp();
exports.getCollections = functions.https.onCall(async (data: any, context: any) => {
cors(data, context, () => {
const collections = admin.firestore().listCollections();
const collectionIds = collections.map((col: { id: any; }) => col.id);
return { collections: collectionIds };
});
});
Then, to make a test, I just print the response using this code in my template :
callCloudFunction() {
this.http
.get(
'https://us-central1-projet-fred.cloudfunctions.net/getCollections')
.subscribe((data: any) => {
console.log(data);
this.firebaseReply = data.text;
});
}
But I got an error in the console. I am not sure I am using this function correctly.. Maybe I have to give parameters to it...
Upvotes: 0
Views: 132
Reputation: 2076
Could be because of the CORS. I would approach this as follows:
For the last point, I get the project id with this
process.env.GCLOUD_PROJECT
based on it, I enable/disable cors.
Upvotes: 1