我对使用新的 firestore 功能感到困惑
https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array
在 firestore 文档中,我有数组“List”和自定义对象“MyObect”
...update("TestArray", FieldValue.arrayUnion("Test"));
工作正常,符合预期。
但是当我尝试这样做时:
MyObject object = new MyObject(Value1, Value2);
...update("List", FieldValue.arrayUnion(object));
我以错误告终
java.lang.IllegalArgumentException: Invalid data. Unsupported type: com.my.app.MyObject
at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:firebase-firestore@@17.1.3:293)
at com.google.firebase.firestore.UserDataConverter.parseScalarValue(com.google.firebase:firebase-firestore@@17.1.3:405)
at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@17.1.3:254)
at com.google.firebase.firestore.UserDataConverter.parseArrayTransformElements(com.google.firebase:firebase-firestore@@17.1.3:419)
at com.google.firebase.firestore.UserDataConverter.parseSentinelFieldValue(com.google.firebase:firebase-firestore@@17.1.3:335)
at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@17.1.3:237)
at com.google.firebase.firestore.UserDataConverter.parseUpdateData(com.google.firebase:firebase-firestore@@17.1.3:171)
at com.google.firebase.firestore.DocumentReference.update(com.google.firebase:firebase-firestore@@17.1.3:239)
编辑 这是我的数据库结构:
/用户数据/用户ID/数据/DataSet01/
在这条路径下,我有一些值,例如:
- 姓名
- 出生
- 性别
- 列表(数组)
出生性别列表(数组)
DocumentReference document= FirebaseFirestore.getInstance().document("UserData").collection("UserID").document("DataSet01");
document.update("TestArray", FieldValue.arrayUnion("test"));
添加字段“TestArray”和任何给定的字符串,在上述“test”的情况下。
但是下面的代码不起作用:
DocumentReference document= FirebaseFirestore.getInstance().document("UserData").collection("UserID").document("DataSet01");
document.update("List", FieldValue.arrayUnion(myObject));
最佳答案
我遇到了同样的问题,发现使用自定义对象不起作用。
所以我尝试了 HashMap<String,Any>
我是 Kotlin
val myObject = HashMap<String,Any>()
myObject["Value1"]="Some Value"
myObject["Value2"]="Some other Value"
document.update("List", FieldValue.arrayUnion(myObject))
在 Java 中
HashMap<String, String> myObject = new HashMap<String, String>();
myObject.put("Value1","Some Value");
myObject.put("Value1","Some Other Value");
document.update("List", FieldValue.arrayUnion(myObject));
希望对您有所帮助:)
关于Android Firestore - 数组中的更新对象导致类型不受支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53400973/