Document to WriteableMap

I am updating the question to make it clearer. From react-native, I call a java method to get a document as follows:

      @ReactMethod
      public void getDocument(String documentId, Promise promise){
    try{
        Document document = database.getDocument(dbName, docId);
        if (document != null){
            // document found. resolve well
            promise.resolve(document);
        }else{
            promise.resolve(null);
        }

    }catch(CouchbaseLiteException e){
      // no exception caught in this block
       logException(e);
        promise.reject(e.getMessage());
    }catch(Exception e){
        //exception caught here
        logExceptions(e);
        promise.reject(e.getMessage());
    }
        
     }

the above fails with the exception:

              java.lang.RuntimeException: Cannot convert argument of type class com.couchbase.lite.Document

again, my guess is Document type couldn’t be resolved back to react-native/Javascript as it is because doing:

                       promise.resolve(documen.toString());

works perfectly because I see the document in javascript in string format as follows exactly:


Document{@0x3e8219105cee7a7-2431-47a0-a25e-605ee039a13a@1-a7ed608736c34cf5860a0d35968e6441e1f089ef(..):obj=>Dictionary{(..)a=>A,b=>B},ranks=>com.couchbase.lite.Array@1397ba,age=>20,objAr=>com.couchbase.lite.Array@5f,active=>true,type=>employee,salary=>1903.93,nl=>null}

But i want to resolve something like a WritableMap. THIs one would work for e.g.:

                      WriteableMap map = new WriteableMap();
                       map.putDouble('salary', 1903.93);
                       promise.resolve(map);

so my question is simply how can I pass a found document back to react-native? Is there a simple way to convert a Document to WriteableMap (of react-native) or perhaps a json?

My friend, none of us, here, can figure out how to help you. You’ve shown no errors and we can’t even figure out, exactly, what it is you are trying to do…

If you can give us a little more detail. we can try to be of assistance.

@rut I do not have specific expertise in the Couchbase SDKs but I noticed just at a high level that WriteableMap and ReadableArray are classes from com.facebook.react.bridge:

while MutableArray returned by document.GetArray is a Couchbase class:

https://docs.couchbase.com/mobile/2.0/couchbase-lite-java/db021/com/couchbase/lite/MutableArray.html

So perhaps the problem is the code is trying to mix apples and oranges without a conversion in between.

@blake.meike sorry i have simplified the question and hopefully it is clearer now.

At the end of the day, what you have is a collection of key value pairs. I’m not familiar with React Native at all, but perhaps there is some way to set a custom conversion method or class to handle the conversion. If not, then converting it to something else first by iterating recursively over the keys and values will do the trick.

Side note: In 3.0 Couchbase has added a toJSON method to convert various objects into a JSON string, which may help you as well.