使用云功能

时间:2017-06-28 19:24:15

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

我有以下数据结构:

enter image description here

如何使用云功能完全删除'天'?

我目前的代码:

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

     exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
    .onWrite(event => {
      var ref = event.data.ref.parent; // reference to the items
      var now = Date.now();
      var cutoff = now - 2 * 60 * 60 * 1000;
      var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
      return oldItemsQuery.once('value', function(snapshot) {
        // create a map with all children that need to be removed
        var updates = {};
        snapshot.forEach(function(child) {
          updates[child.key] = null
        });
        // execute all updates in one go and return the result to end the function
        return ref.update(updates);
      });

functions.database.ref('/days').remove(); // <- /days doesn't get removed

    });

1 个答案:

答案 0 :(得分:1)

您在调用remove之前从函数返回。尝试:

  return oldItemsQuery.once('value', function(snapshot) {
    // create a map with all children that need to be removed
    var updates = {};
    snapshot.forEach(function(child) {
      updates[child.key] = null
    });
    // execute all updates in one go and return the result to end the function
    return ref.update(updates);
  }).then(function() {;
    return functions.database.ref('/days').remove();
  });