Yann92
Yann92

Reputation: 383

Cloud Firestore function in IONIC 4

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...

enter image description here

Upvotes: 0

Views: 132

Answers (1)

michelepatrassi
michelepatrassi

Reputation: 2076

Could be because of the CORS. I would approach this as follows:

  • create the http cloud function
  • test it with postman (cors will not matter, given that is only from browser to server)
  • call the function from angular with disabled cors when developing locally, enable it in production.

For the last point, I get the project id with this

process.env.GCLOUD_PROJECT

based on it, I enable/disable cors.

Upvotes: 1

Related Questions