简体   繁体   English

IONIC 4 中的 Cloud Firestore 功能

[英]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.我是 ionic 和 cloud 功能的新手,我尝试通过一些阅读来实现云功能,但我不确定我是否正确使用它..我想在 firebase 中检索我的数据库的一些集合。

I have a cloud function in firestore to retrieve the collection of my db in my ionic4 app :我在 firestore 中有一个云功能,可以在我的 ionic4 应用程序中检索我的数据库集合:

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...我不确定我是否正确使用了这个函数.. 也许我必须给它参数......

在此处输入图片说明

Could be because of the CORS.可能是因为 CORS。 I would approach this as follows:我会这样处理:

  • create the http cloud function创建http云函数
  • test it with postman (cors will not matter, given that is only from browser to server)用邮递员测试(cors 无关紧要,因为这只是从浏览器到服务器)
  • call the function from angular with disabled cors when developing locally, enable it in production.在本地开发时使用禁用的 cors 从 angular 调用函数,在生产中启用它。

For the last point, I get the project id with this对于最后一点,我用这个获得了项目 ID

process.env.GCLOUD_PROJECT

based on it, I enable/disable cors.基于它,我启用/禁用 cors。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM