firebase 在Users集合中创建好友子集合云Firestore

ddrv8njm  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(98)

我有一个“用户”集合,其注册方法如下:

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)

unhi4e5o

unhi4e5o1#

您将收到以下错误:
异常错误:无效数据。数据必须是Map或合适的POJO对象,但它的类型为:java.lang.String
因为您试图向文档写入一个String值,这实际上是不可能的。根据set()方法参数的官方文档:
要写入文档的数据(例如包含所需文档内容的Map或POJO)。
所以你不可能简单地写一个String,因此出现了这个错误。为了解决这个问题,向set()方法传递一个Map,其中包含属性的名称作为键,并包含该String的实际值作为值。该模式应如下所示:

Firestore-root
   |
   --- USERTABLE (collection)
         |
         --- uid (document)
              |
              --- AMISCOLLECTION (collection)
                     |
                     --- uid (document)
                          |
                          --- fieldName: "StringValue"

字符串
您还可以使用POJO类的对象,就像在前两个共享示例中所做的那样。

相关问题