【问题标题】:Cloud Firestore java.lang.IllegalArgumentException: Invalid data. Unsupported type:Cloud Firestore java.lang.IllegalArgumentException:无效数据。不支持的类型:
【发布时间】:2019-02-12 22:47:23
【问题描述】:

我正在将文档添加到 Firebase 的 Cloud Firestore 中的集合中并不断收到此异常:

java.lang.IllegalArgumentException:无效数据。不支持的类型:com.example.com.myproject.PickUpDate(在字段 pick_up 中找到)

我以以下方式存储对象:

private fun storeData() {
    val db = FirebaseFirestore.getInstance()
    val offerDocument = HashMap<String, Any?>()
    offerDocument.put(Key.CATEGORY, offer.category)
    offerDocument.put(Key.ITEM_TYPE, offer.itemType)
    offerDocument.put(Key.BRAND, offer.brand)
    offerDocument.put(Key.PRICE, offer.price)
    offerDocument.put(Key.PICK_UP_DATE, offer.pickUpPickUpDate)
    offerDocument.put(Key.AVAILABILITY, offer.availability)
    offerDocument.put(Key.COLOR, offer.color)

   offer.images =  adapter?.list?.map { it.toString() }
    offerDocument.put(Key.IMAGES, offer.images)


    // Add a new document with a generated ID
    db.collection("offers")
            .add(offerDocument)
            .addOnSuccessListener { documentReference -> Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.id) }
            .addOnFailureListener { e -> Log.w(TAG, "Error adding document", e) }
}

我的 PickUpDate 课程如下:

data class PickUpDate(var year : Int, var month : Int, var day : Int){}

可能是什么原因?为什么异常是专门针对这个类而不是其他类抛出的?

【问题讨论】:

  • 你有其他类没有任何错误的例子吗?
  • @cricket_007 查看类别类:数据类 Category(var name: String, var id: Int)

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


【解决方案1】:

您没有显示要添加到地图以放入文档的所有其他字段的类型。我的猜测是它们都是原始类型,例如整数或字符串。 PickUpDate 是您定义的自定义类,而不是 Firestore 中文档字段支持的标准类型之一。

如果您想在文档中创建一个字段,该字段包含一个对象,其中包含 PickUpDate 中包含的三个值,您可以将它们放在一个 HashMap 中,然后将该 HashMap 分配给文档中的一个字段:

val map = mapOf<String, Any>(
    "year" to offer.pickUpPickUpDate.year,
    "month" to offer.pickUpPickUpDate.month,
    "day" to offer.pickUpPickUpDate.day
)

offerDocument.put(Key.PICK_UP_DATE, map)

或者,找到其他方法将对象中的值编码为有效的字段值(如时间戳或单个数字)。

【讨论】:

    【解决方案2】:

    您收到以下错误:

    java.lang.IllegalArgumentException:无效数据。不支持的类型:com.example.com.myproject.PickUpDate

    因为在 Firestore 中,您不能为您的 PICK_UP_DATE 属性分配一个不是 supported data type 的值。您的 PickUpDate 课程不受支持。要解决此问题,您需要将该属性的类型更改为受支持的类型之一。

    【讨论】:

    • 一切都好吗,我可以帮助您了解其他信息吗?
    • 非常感谢,我已按照@Doug Stevenson 的建议将自定义类型的对象放在地图中。当然不是很好,但我们必须遵守规则,只放支持的类型
    猜你喜欢
    • 2018-06-25
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2019-11-18
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多