【问题标题】:Associating user data with the data provided by REST API将用户数据与 REST API 提供的数据相关联
【发布时间】:2021-06-15 18:05:12
【问题描述】:

为了更好地了解 React,我制作了一个简单的清单应用程序,让您可以跟踪游戏中的进度并做一些相关的笔记。数据以 JSON 对象的形式保存在 firebase 实时数据库中。对于单个用户(我自己),应用程序按预期工作,但我不知道如何允许多人访问该站点并维护他们自己的清单。

{
  "items": [{
    "id": 0,
    "task": "Task to follow",
    "note": "Strategize",
    "isCompleted": false
  }, {
    "id": 1,
    "task": "Another task",
    "remark": "Wait for second playthrough",
    "isCompleted": true
  }]
}

代码 sn-p 表示带有模拟数据的清单 JSON 对象。假设我设法实现了用户身份验证,我如何将该登录用户与他们唯一的清单数据相关联,而不是每个用户共享相同的清单?对于添加的上下文,用户将只被允许更改“note”和“isCompleted”属性。最佳解决方案是将数据保存在 MongoDB 中,但我很想从您那里读到一些见解,这些见解将使我能够修改现有项目的部分内容,而不是重做整个事情。

【问题讨论】:

  • 您使用的是firebase,您可以将数据的形状修改为> user id > rest of the data>。这样你就可以访问它
  • @ObedMarquezParlapiano 很好地解决了我的问题。太感谢了。这种修改不会导致数据重复吗?假设有 50 个清单对象,10 个用户加起来将有 500 个唯一对象。我是新手,所以我很想阅读您对此的见解。
  • 正确回答。

标签: json reactjs firebase-realtime-database frontend rest


【解决方案1】:

如果您要寻找的是每个登录用户都有自己的清单,您可以将商店的结构修改为如下所示:

{ 'user-id-string-from-logged-in-hash': 
    {
      "items": [{
        "id": 0,
        "task": "Task to follow",
        "note": "Strategize",
        "isCompleted": false
      }, {
        "id": 1,
        "task": "Another task",
        "remark": "Wait for second playthrough",
        "isCompleted": true
      }]
     }
    }

这样您就可以将每个用户隔离并轻松访问。你最终会得到一个大数据库,但这完全没问题。使用 firebase,您正在寻找一个良好的结构和便利性,而不是优化大小,因为 firebase 非常快(它就像一个查找表)。s

如果您希望让多个用户共享和修改清单,您可以执行以下操作:

{
  "items": [{
    "id": 0,
    "task": "Task to follow",
    "note": "Strategize",
    "isCompleted": false,
    "modifiedBy": ['hash-of-user': { < object with the type of modifications the user did or something >}]
  }, {
    "id": 1,
    "task": "Another task",
    "remark": "Wait for second playthrough",
    "isCompleted": true
    "modifiedBy": ['hash-of-user': { < object with the type of modifications the 
    user did or something >}]
  }]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多