【问题标题】:Firestore Database model create with kotlin使用 kotlin 创建 Firestore 数据库模型
【发布时间】:2021-10-23 12:50:29
【问题描述】:

我正在尝试为 Firestore 建模数据库架构。如何创建此数据库模型?

这是我的食谱数据类

data class Foods(
var foodId:String?,
var foodName:String,
var foodCategory:String,
var foodCalory:Int,
var foodIngredients:String,
var foodRecipe:String,
var foodCookingTime:Int,
var foodImg:String?)

我的用户 ID 得到了这个

  val user : FirebaseUser? = auth?.currentUser
    val userID: String = user?.uid.toString()

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: android firebase kotlin google-cloud-platform google-cloud-firestore


【解决方案1】:

正如我在您的屏幕截图中看到的,您的文档 (vp0q ...) 包含一组 Foods 对象。如果您希望能够将该文档映射到特定类的对象,则应考虑使用以下类声明:

data class User (recipe: Array<Foods>)

现在,要阅读该文档,您应该使用以下参考:

val uid = FirebaseAuth.getInstance().currentUser?.uid
val rootRef = FirebaseFirestore.getInstance()
val usersRef = rootRef.collection("users")
usersRef.document(uid).get()
    .addOnSuccessListener { document ->
        if (document != null) {
            val recipe = document.toObject<Foods>().recipe
            //Do what you need to do with your recipe array
        } else {
            Log.d(TAG, "No such document")
        }
    }
    .addOnFailureListener { exception ->
        Log.d(TAG, "get failed with ", exception)
    }

【讨论】:

  • 嗨@AlexMamo,非常感谢您的回复。但我问我如何创建这个数据库模型。我手工设计了这张图片中的模型,作为 Firestore 中的示例。
  • @Ayse.S 我无法理解您想要实现的目标,您能否编辑 OP 并更具体地说明一下?
  • 要实现该数据库结构,您应该使用我的答案中存在的User 类。试过了吗?
  • 嗨@AlexMamo 是的,亚历克斯你的回答对我很有帮助。谢谢
  • 很高兴听到这个消息,Ayse.S.
猜你喜欢
  • 2016-12-21
  • 2020-12-16
  • 2020-10-10
  • 2020-06-20
  • 1970-01-01
  • 2018-09-03
  • 2019-01-03
  • 2019-04-17
  • 1970-01-01
相关资源
最近更新 更多