【问题标题】:Flutter Firebase Functions Error: Database not definedFlutter Firebase 函数错误:未定义数据库
【发布时间】: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


    【解决方案1】:

    DateTime 不是有效的 JavaScript 对象或标识符。如果您想使用日期和时间,则需要使用 Date,因为您位于拥有 DateTime 的正上方的行中。您可能应该查看 Date 的 JavaScript 文档以了解其工作原理。

    【讨论】:

      猜你喜欢
      • 2019-07-26
      • 2021-06-17
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2012-03-09
      相关资源
      最近更新 更多