【发布时间】: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