Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore update fails with IllegalArgumentException: Invalid data. Unsupported type

When calling

FirebaseFirestore.getInstance().collection("myCollection").document("doc1").update("field1",myObject);

I get the error:

IllegalArgumentException: Invalid data. Unsupported type: com.myProg.objects.MyObject (found in field field1)

Even though I can add myObject to firestore when it is part of myDoc using Set method without a problem.

MyObject class (The simplest example):

public class MyObject{
   public int i;
}

Edit: my DB Structure before attempting:

myCollection ->

doc1:

field0 - "3"

field1 - null

also tried it without field1

like image 879
CaptainNemo Avatar asked Jan 16 '18 19:01

CaptainNemo


2 Answers

So the only way, to update as of now is by using a map. In your case it should look like

Map<String, Object> updateMap = new HashMap();
updateMap.put("field1.i", myObject.i);

FirebaseFirestore.getInstance().collection("myCollection")
.document("doc1").update(updateMap);

I think Firestore should really update the APIs to facilitate updating of nested objects as a whole.

like image 57
Debanjan Avatar answered Nov 17 '22 18:11

Debanjan


In my case i wanted to update the complex object in the firestore.

public class UserInitialModel { private List<ServiceItemModel> servicesOffered; }

public class ServiceItemModel{
private String serviceName;
private String price;
}

it was giving me error when i try to update java.lang.IllegalArgumentException: Invalid data. Unsupported type:

I Solved it by by using map

List<Map<String,Object>> list=new ArrayList<>();
        for (ServiceItemModel model:
        mUserInitialPresenter.getUserInitialModel().getServicesOffered()) {
            Map<String,Object> servicesOffered=new HashMap<>();
            servicesOffered.put("serviceName",model.getServiceName());
            servicesOffered.put("price",model.getPrice());
            list.add(servicesOffered);
        }
        dataMap.put("servicesOffered",list);
like image 2
Adarsh Binjola Avatar answered Nov 17 '22 18:11

Adarsh Binjola