Javascript Firebase将数据链接到用户

Javascript Firebase将数据链接到用户,javascript,firebase,firebase-realtime-database,firebase-authentication,Javascript,Firebase,Firebase Realtime Database,Firebase Authentication,如何将数据链接到用户 在我的博客上有一个评论部分,用户可以在那里发表评论 结构看起来像这样: - posts - My First Post - content: "a big string of the post content" - data: "Date Created" - image: "Image URL" - imagecaption: "Image Caption" - comments - ??? - comments

如何将数据链接到用户

在我的博客上有一个评论部分,用户可以在那里发表评论

结构看起来像这样:

- posts
  - My First Post
    - content: "a big string of the post content"
    - data: "Date Created"
    - image: "Image URL"
    - imagecaption: "Image Caption"
    - comments
      - ???
- comments
  - HbsfJSFJJSF (Comment ID)
    - user: (User Reference)
    - comment: "Nice Blog!"
- comments
  - HbsfJSFJJSF (Comment ID)
    - user: user_uid
    - comment: "Nice Blog!"
现在在评论中,有这样的东西会很好:

- posts
  - My First Post
    - content: "a big string of the post content"
    - data: "Date Created"
    - image: "Image URL"
    - imagecaption: "Image Caption"
    - comments
      - ???
- comments
  - HbsfJSFJJSF (Comment ID)
    - user: (User Reference)
    - comment: "Nice Blog!"
- comments
  - HbsfJSFJJSF (Comment ID)
    - user: user_uid
    - comment: "Nice Blog!"
现在我明白我可以这样做:

- posts
  - My First Post
    - content: "a big string of the post content"
    - data: "Date Created"
    - image: "Image URL"
    - imagecaption: "Image Caption"
    - comments
      - ???
- comments
  - HbsfJSFJJSF (Comment ID)
    - user: (User Reference)
    - comment: "Nice Blog!"
- comments
  - HbsfJSFJJSF (Comment ID)
    - user: user_uid
    - comment: "Nice Blog!"
但问题是(?)如果账户被删除(我有这个功能),评论就不会被删除


是否有适当的方法将数据(注释)链接到用户,以便在删除用户帐户时删除注释,或者至少有方法在删除用户帐户时删除与用户对应的注释?

Firebase实时数据库中没有内置的托管链接支持。因此,这取决于您编写的代码

这通常意味着您将拥有一个处理用户删除的中心函数(可能是Firebase的云函数)。然后,该函数调用Firebase身份验证来删除该用户,并更新数据库以删除对该用户的引用


还有一个开源项目,旨在使这种清理操作更简单/更可靠:

在每个注释节点下使用
user:user\uid
的想法会奏效,称为

使用此方法,您可以通过执行查询来获得
用户
值等于当前用户ID的所有注释,并删除每个注释,如:

var commentsRef = firebase.database().ref('comments');
var userId = firebase.auth().currentUser.uid;

commentsRef.orderByChild('user').equalTo(userId).once('value', function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var commentKey = childSnapshot.key;
    commentsRef.child(commentKey).remove();
  });
});

为了确保删除用户时在幕后执行此操作,您可以将上述逻辑移动到由删除请求触发的中。

我可以执行类似“COMMENT\u REF.child(“users”).child(authData.uid)”的操作吗?但是,在用户删除其帐户后,我如何删除注释谢谢您的回答。我怀疑这一点,但无法面对现实。因为我在这上面找到了其他的消息来源。谢谢:)啊,这也行,我不知道有一个用户数据清理项目,我得看看!云函数在谷歌服务器上运行JavaScript代码以响应事件(发生在Firebase或其他地方的事件)。因此,当涉及服务器时,您不必对其进行配置。您只需编写代码,并在需要运行时告知云函数。这些函数在Firebase的服务器上运行,因此无需自定义实现。上面的例子将迭代每个用户ID与用户ID匹配的评论,并将其删除。@FrankvanPuffelen哈哈,我们就像同步游泳的人!