如果我知道记录,如何在angularfire中获取文档名称?

时间:2014-10-31 05:35:11

标签: json angularjs firebase behavior angularfire

我有一些像

这样的记录
{
  "posts" : {
    "1" : {
      "text" : "sometext",
      "title" : "test"
    }
  },
  "user_posts" : {
      "-J_ZT9vZJ4CcXh3PuiwR" : "1"
    }
}

如何在" user_posts"中找到文档的名称?通过使用记录" 1" ? 我需要删除它。我正在做什么:

$firebase(ref.child('user_posts')).$remove($indexFor(post.$id));
//post.$id is a id os post on posts, by using ng-repeat...

但我无法取消。 console.log()始终返回-1。我想我做错了什么......

1 个答案:

答案 0 :(得分:3)

更新

我最初误解了你的问题。我现在明白,当您从"-J_ZT9vZJ4CcXh3PuiwR"移除节点user_posts时,您想要从1删除节点posts

您无法使用$indexFor。由于您尝试匹配的对象位于两个不同的集合中,因此posts -> 1无法与user posts -> -J_ZT9vZJ4CcXh3PuiwR相同。

相反,你必须想出另一种方法来匹配它们。最简单的可能是将帖子ID设置为user_posts

的优先级
userPostsRef.push().setWithPriority(1, 1);

因此,它将值和优先级都设置为1。

然后,您可以使用查询按优先级查找正确的条目:

userPostsRef.startAt(1).endAt(1).once('child_added', function(snapshot) {
    snapshot.ref().remove();
});

这使用Firebase的标准Web / JavaScript API,可与AngularFire互操作。如果您更愿意在AngularFire中看到它,您必须自己翻译它。

原始答案

要使用$ firebase使用AngularFire删除项目,您只需传入您尝试删除的节点的名称:

$firebase(ref.child('user_posts')).$remove(post.$id);

请参阅:https://www.firebase.com/docs/web/libraries/angular/guide.html#section-basics