Android and Realm - Could not resolve encoder for end type

I’m attempting to use a Realm function in my Kotlin Android application, but I’m getting the error ‘Could not resolve encoder for end type’.

Here is where I’m calling the function:

val completableDeferred = CompletableDeferred<App.Result<Document>>()
val functions: Functions = app.getFunctions(app.currentUser())
//user.email and user.name are strings
functions.callFunctionAsync("assignNewUser", listOf(user.email, user.name), Document::class.java){
    completableDeferred.complete(it)
}

And here is the Realm function:

exports = async function(email, name) {
    const collection = context.services.get("mongodb-atlas").db("quotes").collection("User");
    const filter = {email: email};
    const memberToAssign = await collection.findOne(filter);
    if (memberToAssign == null) {
        return {error: `User ${email} not found`};
    }
    try {
        return await collection.updateOne(
            {_id: memberToAssign._id},
            {$set: {
                name: name,
                }
            });
    } catch (error) {
        return {error: error.toString()};
    }
};

Does anyone know what this error means?

Also, a side-note. I’m calling this function after I’ve registered a user, because I haven’t found a way to register a user while passing in extra parameters like a name or a profile picture. I’m registering users using the App.emailPassword.registerUserAsync(email, password) method.

Curiously, when I try to run a function like this, I hit no problems. It’s a little tricky without seeing the code for completableDeferred or your imports, but I would guess that you’re using the wrong type of Document. Check the imports at the top of your file for:

import org.bson.Document

If you’ve imported some other kind of Document class, Realm probably won’t know how to write data into that class. Let me know if this works!

Thanks for the reply. The Document I’m using is in fact an org.bson.Document.

The rest of the Kotlin code is here, sorry, I didn’t think it relevant.

val result = completableDeferred.await()
if (!result.isSuccess) {
    val exception = RegisterException()
    result.error.errorMessage?.let { exception.errorMessage = it }
    throw exception
}

Edit: And my imports:

import io.realm.mongodb.App
import io.realm.mongodb.functions.Functions
import joe.barker.domain.boundary.data.RegisterData
import joe.barker.domain.entity.User
import joe.barker.domain.exception.MyException
import joe.barker.domain.exception.RegisterException
import kotlinx.coroutines.CompletableDeferred
import org.bson.Document

What version of the Realm Android SDK are you using? I just tried with 10.0.0 and 10.3.1 with mostly the same code as you provided here and I didn’t have any issues.

What is the type of RegisterException.errorMessage? I wonder if your function is throwing an error and you actually need to change

    result.error.errorMessage?.let { exception.errorMessage = it }

to…

    result.error.errorMessage?.let { exception.errorMessage = it.error.error }

The RegisterException is my own, of type Throwable and has an errorMessage val.

But, quite strangely my error has changed to FunctionError: update not permitted. No idea why, I haven’t changed anything! I would assume my User needs to have the permission, where would I set this?

That’s largely dependent on your app, but you can get started by reading up on permissions here. You can also check out some example query roles here. Depending on your application you might not need anything other than allowing all reads and writes to a specific collection in a specific database.

Great. Thanks for the help.

If that encoder error appears again I’ll mention it, it’s odd that it’s stopped appearing.

So I’ve set up a schema with rules that a user can read/write their own data, but I’m still getting the update not permitted error.

{
  "roles": [
    {
       "name": "usersCanCRUDTheirOwnData",
       "apply_when": {
         "_id": "%%user._id"
       },
      "insert": true,
      "delete": true,
      "search": true,
      "read": true,
      "write": true,
      "fields": {},
      "additional_fields": {}
    }
  ],
  "filters": [],
  "schema": {
    "title": "User",
    "properties": {
      "_id": {
        "bsonType": "string"
      },
      "_partition": {
        "bsonType": "string"
      },
      "canReadPartitions": {
        "bsonType": "array",
        "items": {
          "bsonType": "string"
        }
      },
      "canWritePartitions": {
        "bsonType": "array",
        "items": {
          "bsonType": "string"
        }
      },
      "email": {
         "bsonType": "string"
      },
       "name": {
         "bsonType": "string"
      },
      "profilePicture": {
        "bsonType": "binData"
      }
    },
    "required": [
       "_partition",
       "email",
       "name"
    ]
  }
}

I can’t for the life of me figure out what I’m missing. Maybe the problem will be mysteriously replaced with another one again when I ask on here, ha :face_with_monocle:

Edit: I’ve also tried this without the apply_when