云功能未执行

时间:2018-07-19 07:28:43

标签: node.js firebase firebase-realtime-database google-cloud-functions

我正在使用公交车预订应用程序。我必须存储特定时间段内任何人的详细信息,但是我不想每天都继续存储数据。因此,我想执行可以删除的云功能每天的数据。我的数据库结构为enter image description here

例如,如果用户在3:30预订乘车,他的凭据将存储在“ 3:30”下。但是我想在当天结束后删除“ 3:30”下的数据。为此,我想使用云功能

我用于云功能的index.js文件是

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

// Cut off time. Child nodes older than this will be deleted.
    const CUT_OFF_TIME = 15 * 1000; // in milliseconds.(for testing purpose I want to delete items older than 15 seconds)

/**
 * This database triggered function will check for child nodes that are older than the
 * cut-off time. Each child needs to have a `timestamp` attribute.
 */
exports.deleteOldItems = 
functions.database.ref('/3:30/{pushId}').onWrite((change) => {
  const ref = change.after.ref.parent; // reference to the parent
  const now = Date.now();
  const cutoff = now - CUT_OFF_TIME;
  const oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
  return oldItemsQuery.once('value').then((snapshot) => {
    // 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);
  }); 
});

但是当我部署云功能并且通过我的应用程序将用户添加到该路径时,数据不会被删除,但是当我通过firebase控制台添加用户时,数据会立即被删除。我是node的新手。 js,我什至不知道该代码是否正确。有人可以告诉我此功能有什么问题吗?

0 个答案:

没有答案