【问题标题】:is subcollection the best way to create a "friendlist" of players?子集合是创建玩家“好友列表”的最佳方式吗?
【发布时间】:2019-12-14 04:41:51
【问题描述】:

我很难处理子集合(这是我的第一个 flutter/firebase/nosql 项目)

我想在玩家集合中实现一个“朋友列表”,我需要一个与朋友列表中其他玩家的子集合,这是最好的方法吗?如果是,我如何将子集合添加到文档中?

final CollectionReference playerCollection =
      Firestore.instance.collection('player');

Future updatePlayerData(
    String nickname,
    int gamesWon,
    int points,
  ) async {
    return await playerCollection.document(uid).setData({
      "nickname": nickname,
      "gamesWon": gamesWon,
      "points": points,
    });
  }

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    你可以像这样在document里面制作subCollection

    final CollectionReference playerCollection =
    Firestore.instance.collection('player');
    
    Future updatePlayerData(
        String nickname,
        int gamesWon,
        int points,
      ) async {
        return await playerCollection.document(uid).collection(frdUid).document(frdUid).setData({    
    //frdUid is unique id
          "nickname": nickname,
          "gamesWon": gamesWon,
          "points": points,
        });
      } 
    

    【讨论】:

    • .collection(frdUid) 非常不寻常,通常是个坏主意。原因是您无法在客户端 SDK 中查询集合名称,因此您几乎总是使用硬编码的集合名称。更可能和惯用的方法是.collection('friends').document(frdUid)...
    • 您是否按照上面评论中所说的那样尝试过?如果是,那么我会更新我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    相关资源
    最近更新 更多