我有一个“用户”集合,其注册方法如下:
public class UserHelper {
public static final String COLLECTION_NAME = "USERTABLE";
public static CollectionReference getUserCollection(){
return FirebaseFirestore.getInstance().collection(COLLECTION_NAME);
}
public static Task<Void> createUser(String uid, String email, String password, String pseudo, String photo_url,boolean statut){
Usermodele usermodele = new Usermodele(uid,email,password,pseudo,photo_url,statut);
return UserHelper.getUserCollection().document(uid).set(usermodele);
}
对于这个类,我想创建一个子集合“ AmisHelper”,我这样做是为了创建子集合,但是似乎有些事情没有进行。
public class AmiHelper {
private static final String COLLECTION_NAME = "AMISCOLLECTION";
public static CollectionReference getAmiCollection(){
return FirebaseFirestore.getInstance().collection(COLLECTION_NAME);
}
public static Task<Void> createAmi(String idDemandeur, String idReceveur, String pseudo, String photo_url, String date_amitie){
AmisModele amisModele = new AmisModele(idDemandeur,idReceveur,pseudo,photo_url,date_amitie);
return UserHelper.getUserCollection().document(idDemandeur).collection("AMISCOLLECTION").document(idReceveur).set(amisModele);
}
在使用时,我做到了
holder.boutonAjouter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(holder.itemView.getContext(), "clic position"+ position, Toast.LENGTH_SHORT).show();
UserHelper.getUserCollection().document(uid).collection("AMISCOLLECTION").document(uid).set(uid,uid,"","","");
}
});
我可能做了一些异常的事情
当我点击“添加”按钮时,我有这个提示:
java.lang.IllegalArgumentException: Invalid data. Data must be a Map<String, Object> or a suitable POJO object, but it was of type: java.lang.String
at com.google.firebase.firestore.UserDataReader.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@21.4.1:234)
at com.google.firebase.firestore.UserDataReader.parseSetData(com.google.firebase:firebase-firestore@@21.4.1:75)
at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:166)
at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@21.4.1:146)
at com.google.firebase.firestore.CollectionReference.add(com.google.firebase:firebase-firestore@@21.4.1:121)
at com.devolab.recapitulatif.adapter.TabAbonneAdapter$1.onClick(TabAbonneAdapter.java:79)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
答案 0 :(得分:2)
您遇到以下错误:
java.lang.IllegalArgumentException:无效的数据。数据必须是Map或合适的POJO对象,但其类型为:java.lang.String
因为您正在尝试向文档中写入String值,但这实际上是不可能的。根据有关set() method parameters的官方文档:
要写入文档的数据(例如包含所需文档内容的Map或POJO)。
因此您无法简单地编写String,因此会出现该错误。要解决此问题,请将Map传递给set()方法,该Map包含属性名称和该String的实际值作为键。模式应如下所示:
Firestore-root
|
--- USERTABLE (collection)
|
--- uid (document)
|
--- AMISCOLLECTION (collection)
|
--- uid (document)
|
--- fieldName: "StringValue"
您还可以使用POJO类的对象,就像在前两个共享示例中所做的那样。