Firebase 数据库 remove()“不是函数”

Tho*_*hoe 2 firebase-realtime-database

此代码有效:

firebase.database().ref($scope.language).orderByChild('word').equalTo($scope.word).once('value')
  .then(function(snapshot) {
    console.log(snapshot.val());
  })
Run Code Online (Sandbox Code Playgroud)

它记录对象及其键。

此代码不起作用:

firebase.database().ref($scope.language).orderByChild('word').equalTo($scope.word).remove()
  .then(function(snapshot) {
    console.log("Removed!");
  })
Run Code Online (Sandbox Code Playgroud)

错误信息是:

TypeError: firebase.database(...).ref(...).orderByChild(...).equalTo(...).remove is not a function
Run Code Online (Sandbox Code Playgroud)

文档使remove()看起来很简单。我错过了什么?

Fra*_*len 6

只有知道数据在 JSON 树中的特定位置后,才能加载数据。要确定该位置,您需要执行查询并遍历匹配结果:

firebase.database().ref($scope.language).orderByChild('word').equalTo($scope.word).once("value").then(function(snapshot) {
  snapshot.forEach(function(child) {
    child.ref.remove();
    console.log("Removed!");
  })
});
Run Code Online (Sandbox Code Playgroud)

如果您只想在删除所有内容后登录,则可以使用Promise.all()

firebase.database().ref($scope.language).orderByChild('word').equalTo($scope.word).once("value").then(function(snapshot) {
  var promises = [];
  snapshot.forEach(function(child) {
    promises.push(child.ref.remove());
  })
  Promise.all(promises).then(function() {
    console.log("All removed!");
  })
});
Run Code Online (Sandbox Code Playgroud)