【问题标题】:Cloud Firestore function in IONIC 4IONIC 4 中的 Cloud Firestore 功能
【发布时间】:2020-03-27 00:13:54
【问题描述】:

我对离子和云功能很陌生,我尝试通过阅读来实现云功能,但我不确定我是否正确使用它。我想在 firebase 中检索我的数据库的一些集合。

我在 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 };
    });

});

然后,为了进行测试,我只需在模板中使用此代码打印响应:

  callCloudFunction() {
    this.http
      .get(
        'https://us-central1-projet-fred.cloudfunctions.net/getCollections')
      .subscribe((data: any) => {
        console.log(data);
        this.firebaseReply = data.text;
      });
  }

但是我在控制台中遇到了错误。我不确定我是否正确使用了这个函数。也许我必须给它参数...

【问题讨论】:

    标签: angular firebase google-cloud-firestore ionic4


    【解决方案1】:

    可能是因为 CORS。我会这样处理:

    • 创建http云函数
    • 用 postman 测试它(cors 无关紧要,因为它只是从浏览器到服务器)
    • 在本地开发时从禁用cors的角度调用函数,在生产中启用它。

    最后一点,我用这个得到项目ID

    process.env.GCLOUD_PROJECT
    

    基于它,我启用/禁用 cors。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 2020-05-21
      • 1970-01-01
      • 2021-09-07
      • 2018-12-27
      • 2020-10-15
      • 2020-03-14
      相关资源
      最近更新 更多