【问题标题】:Node doesn't get removed from Firebase Database?节点没有从 Firebase 数据库中删除?
【发布时间】:2017-06-17 09:48:09
【问题描述】:

我有以下数据结构:

很遗憾,“days”并未使用以下代码从数据库中删除。

我当前的代码:

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

     exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}') //irrelevant to the question
    .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);
  }).then(function() {;
    return functions.database.ref('/days').remove(); // /days doesn't get removed!
  });

    });

【问题讨论】:

  • 你试过没有斜线和子方法吗? functions.database.ref.child('days').remove();
  • @MaduraPradeep 是的,我试过了。它不起作用。解决问题的答案将立即获得赏金!
  • 使用 Firebase 类,而不是 Firebase 函数。 ref = new Firebase("myfirebase.com"); ref.child(key).remove();
  • @MaduraPradeep 我只想删除“days”节点。
  • days 不是你的根节点吧?

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


【解决方案1】:

你可以这样做:

let updates = {};
updates['/days'] = null;

firebase.database().ref().update(updates);

参见docs中的“更新或删除数据”;

在您的代码中:

.then(function() {;
    let updates = {};
    updates['/days'] = null;

    return firebase.database().ref().update(updates)
});

编辑:试试这样的:

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

...

let updates = {};
updates['/days'] = null;
defaultDatabase.ref().update(updates);

参考:https://firebase.google.com/docs/database/admin/start

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 2017-12-26
    • 2021-05-15
    • 2018-04-12
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    相关资源
    最近更新 更多