【问题标题】:Cloud Functions >Firebas Realtime database index.jsCloud Functions >Firebase 实时数据库 index.js
【发布时间】:2019-10-03 12:28:29
【问题描述】:

我在 Android 应用程序中使用 Firebase 实时数据库,并且有这样的数据: 我想删除所有用户的“点”:标签。

实时数据库

我想用 Cloud Functions 做到这一点。我不知道在 index.js 中放入什么代码

云函数 IMG

    const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

/**
 * @function HTTP trigger that, when triggered by a request, checks every message of the database to delete the expired ones.
 * @type {HttpsFunction}
 */
exports.removeOldMessages = functions.https.onRequest((req, res) => {
    const timeNow = Date.now();
    const messagesRef = admin.database().ref('/users');
    messagesRef.once('value', (snapshot) => {
        snapshot.forEach((child) => {
            if ((Number(child.val()['point'])) <= timeNow) {
                child.ref.set(null);
            }
        });
    });
    return res.status(200).end();
});

【问题讨论】:

  • 欢迎来到 Stack Overflow。您可以学习入门文档firebase.google.com/docs/functions/get-started 和解释如何处理实时数据库触发器的文档firebase.google.com/docs/functions/database-events。如果您的代码遇到任何问题,请分享此代码,我们将能够为您提供帮助。
  • 我在上面写了我的代码。但它给出了这样的错误。 “错误:FIREBASE 致命错误:无法解析 Firebase url。请使用 https://.firebaseio.com”
  • 什么时候出现这个错误?部署时(如入门帮助中所述)或调用 HTTP 云函数时?
  • 是的,我搞错了
  • 但是什么时候你会收到这个错误吗?什么时候做哪个动作?在哪个错误日志中?

标签: firebase firebase-realtime-database google-cloud-platform google-cloud-functions


【解决方案1】:

您不能使用 Cloud 控制台编写使用 firebase-functions 模块的函数。您需要通过 Firebase CLI 部署代码。 CLI 将确保函数环境设置正确,以便 Firebase Admin SDK 可以使用正确的值进行初始化。 Cloud 控制台不会为您执行此操作。

【讨论】:

  • 我要写什么代码来得到我想要的结果?你能帮忙吗?
猜你喜欢
  • 2017-09-28
  • 2019-09-25
  • 1970-01-01
  • 2018-08-19
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多