Mongodb Realm every function produces unexpected error, nothing shows in logs

My functions just end with unexpected error every time and do not show anything in the Realm logs (I turned on logs in each of the function’s settings). I tried user authentication functions, system functions, runAsSystemUser=true, etc.

My setup is Atlas DB, Realm Web App, connect to Realm from Android Java SDK. I have users set up and log in with email and password from Java SDK before running a function. I initialize Realm and the MongodbApp. then call the function with functions.callFunctionAsync(name, parameters, callback). Function codes are very simple and don’t produce errors when I run them directly from Realm UI.

Example functions I tried:

exports = async function(arg){
        let collection = context.services.get("mongodb-atlas").db(<Database>).collection(<Collection>);
        let res = await collection.find({_id:BSON.ObjectId(<ID that exists>}).toArray();
        res = "found: " + res;
        if( context.user ){
          res += "\nUsername is " + context.user.toString();
        }
        res += "\nRunning as system user: " + context.runningAsSystem() + ".";
        return res;
};

and

exports = async function(arg){
  let collection = context.services.get("mongodb-atlas").db(<DB>).collection(<Collection>);
        
        let erg = await collection.updateOne({_id:BSON.ObjectId(<some id that exists>)}, {name:"New Name"});
        res = "Changed #: " + erg.modifiedCount;
        return res;
  
};

*** FINALLY FIXED ***
It’s about how you call the function. You have to put a List-object in the ‘parameters’ parameter (which is the second in the java sdk) of the callFunction[Async] function. Even if your Realm function is defined without parameters (e.g. just exports = function() {...} or exports = async function() {...}) you can NOT put null as the ‘parameters’. The simplest thing to put is new ArrayList(), like this:

functions.callFunctionAsync('functionName', new ArrayList<String>(), ReturnType.class, new App.Callback<ReturnType>() {...});

No hate but I think it’s so sad that simple stuff like this is not stated in the documentations.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.