推送新节点时出现Firebase错误:无法解析具有类class(Kotlin)的节点

时间:2019-07-01 09:17:19

标签: firebase kotlin

我有一个映射(Map<String, Any>),它是Gson对象序列化的结果。

这是将地图打印到控制台时的结果。

{canAccessAdminList=false, canChangeAdminList=true, canChangeVotesData=true, canSendEmailReport=true, email=admin@example.com, getMonthlyReport=true, isActive=true, password=test, username=admin}

原始类是这样的:

class AdminModel {

    var id: Int? = null
    var username: String = ""
    var password: String = ""
    var email: String = ""
    var lastLogin: Date? = null

    var canAccessAdminList: Boolean = false
    var canChangeAdminList: Boolean = false
    var canSendEmailReport: Boolean = false
    var canChangeVotesData: Boolean = false
    var isActive: Boolean = true
    var getMonthlyReport: Boolean = true

}

注意:Gson不会序列化idlastLogin,因为在这种情况下它们为空。

当我尝试在Firebase中这样写时:

ref.push().setValue(map) { error, ref -> }

我得到了错误:

com.google.firebase.database.DatabaseException: Failed to parse node with class class com.example.activities.NewUserActivity$setListener$2$1
        at com.google.firebase.database.snapshot.NodeUtilities.NodeFromJSON(com.google.firebase:firebase-database@@17.0.0:103)
        at com.google.firebase.database.snapshot.NodeUtilities.NodeFromJSON(com.google.firebase:firebase-database@@17.0.0:28)
        at com.google.firebase.database.snapshot.PriorityUtilities.parsePriority(com.google.firebase:firebase-database@@17.0.0:39)
        at com.google.firebase.database.DatabaseReference.setValue(com.google.firebase:firebase-database@@17.0.0:207)
        ....

为什么会这样?我从other answer获悉,这仅在使用updateChildren而非setValue时发生。或者,也因为路径中的某些地方,有些值没有直接映射到JSON值中。但是我只使用字符串,整数和布尔值。是否有任何这些不能直接映射到JSON?即使直接提供到setValue的映射(例如:mapOf("username" to "hello", "password" to "world")),我仍然会遇到相同的错误。请帮忙。

0 个答案:

没有答案