【发布时间】:2020-07-09 16:04:31
【问题描述】:
我为我的数据库创建了一个删除 oldFiles 函数,用于从我的聊天消息中删除节点。我使用了 Firebase 提供的示例函数并对其进行了更新以适合我的使用。我的数据库结构是 databaseName/messages/{pushId},我添加了 const functions = require('firebase-functions') 和 const admin = require('firebase-admin') 和 admin.initializeApp()。这是我所拥有的......
exports.deleteOldItems = functions.database.ref('messages/{pushId}').onWrite(async (change) => {
const ref = change.after.ref.parent; // reference to the parent
const now = Date.now();
const cutoff = (DateTime.now().millisecondsSinceEpoch - CUT_OFF_TIME);
const oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
const snapshot = await oldItemsQuery.once('value');
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
当我查看我的函数日志时,我收到以下错误...
ReferenceError: DateTime 未定义 在exports.deleteOldItems.functions.database.ref.onWrite (/srv/index.js:17:18) 在 cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) 在 /worker/worker.js:825:24 在 在 process._tickDomainCallback (internal/process/next_tick.js:229:7)
我的函数以状态结束:错误。对可能发生的事情有任何想法吗?
【问题讨论】:
标签: javascript firebase firebase-realtime-database google-cloud-functions