Show collection content with a function

Hi everybody,

I work on a student project and discover the MongoDB world (I go back to school at 35 years old !!! :D)

After build a simple android app and finally connect my App to Realm through email/password authentification, I would like to show a part of an Atlas collection.
At the end, it will be to show just a collection’s array but for begin, and understand how it work … show a collection should be good.

But …of course … it doesn’t work and I don’t understand why.

I have a collection called “consumer” with 2 consumers inside.
consumer 1 & 2
- _Id
- email
- password

I would like to show one of the two consumers when i give the email adress.
For this, I wrote :

public HomeViewModel() {
mText = new MutableLiveData<>();

User user = ClientApp.currentUser();

Functions functionsManager = ClientApp.getFunctions(user);
List<String> args = Arrays.asList("florian.vigier@gmail.com");

functionsManager.callFunctionAsync("GetClient", args, String.class, result -> {
    if (result.isSuccess()) {
        Log.v("Collection Found", "La collection est trouvée : " + result.get());
        UserContent = result.get();
    } else {
        Log.e("Collection Not Found", "La collection n'est pas trouvée : " + result.getError());
    }
});

mText.setValue(UserContent);
}

This code call the following function :

exports = function(arg){
let collection = context.services.get(“4proj”).db(“4projDB”).collection(“consumer”);
return collection.findOne({email: arg});
};

And I have the following error message :

E/Collection Not Found: La collection n’est pas trouvée : BSON_DECODING(realm::app::CustomError:1102): Error decoding value {“value”:null}
org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is NULL.
at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:690)
at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:722)
at org.bson.AbstractBsonReader.readString(AbstractBsonReader.java:457)
at org.bson.codecs.StringCodec.decode(StringCodec.java:39)
at org.bson.codecs.StringCodec.decode(StringCodec.java:28)
at io.realm.internal.jni.JniBsonProtocol.decode(JniBsonProtocol.java:87)
at io.realm.mongodb.FunctionsImpl.invoke(FunctionsImpl.java:65)
at io.realm.mongodb.functions.Functions$1.run(Functions.java:146)
at io.realm.internal.mongodb.Request$1.run(Request.java:57)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)

I suppose that the return is not String, but I’m not really sure.

Do someone may help me to understand ?

Hi @Florian_Vigier,

I notice that you’re getting this error:

Just to confirm – is your MongoDB service in your Realm app named 4proj and does the associated cluster contain a database named 4projDB which in turn contains a collection named consumer?

If you run the Realm function directly from the Realm UI, does it produce the expected results?

Hi Andrew,

Thanks for your answer and your time.

For be more cluear :
My Realm app is nammed “ClientApp”
“4proj” is my Atlas Cluster.
Inside this cluster, I have a collection named “consumer”.

So i would like say : YES !

If I run manually this function trough the Realm UI, and just add argument value by export command, the function work well.
I did the same function which modify an account in the consumer collection. This function work well also through Realm UI but not when i try to call it with android program.

I guess the issue is on the Android code and not on the function code.

Thanks @Florian_Vigier.

@Mohit_Sharma could you please take a look at the Android code?

1 Like

@Andrew_Morgan: Thanks for tagging in.

@Florian_Vigier: Would it be possible for you to share your GitHub public repo url with us, so that I can have look and get back to you.

1 Like

Hello @Mohit_Sharma,

Thank you for your answer and your proposition.

Of course it’s possible.

So please find below my GitHub link.

As it’s completely new for me to work on an Android application, work with a database and even work in Javascript … feel free to tell me what’s not good. It will be every time helpfull.

@Florian_Vigier: Thanks for sharing the same, had a quick look at the project but couldn’t find the above code in the repo, therefore can’t help.

Although I have raised a PR for fixing an issue that I found and had to fix it in order to run & validate the app.

It’s normal that you couldn’t find the code shared before … this code is a Realm Function declared on the Realm web site.

In this case, the function name is GetClient and have just to show a result.

In the same way, this function have to edit a user.

Both doesn’t work when called but the application but work well by the UI.

Is it enough to help me ? if not a can try to give you access to the project directly since it’s just a scool work with nothing confidential !!! :smiley:

Ps: Thanks for the fix. I will have a look this evening.

For be simple, I would like to do the same as https://docs.mongodb.com/realm/sdk/android/examples/call-a-function/#call-a-function-by-name

So first, I do the same as the example, copy the code even if it’s a little bit stupid :smiley: and create a function called “sum”

The function :

But when I call the function, it doesn’t work since it don’t find the function even if the function exist.

My debug mod > show the function which has been called but seems to doesn’t exit.

@Florian_Vigier: I just tried the same thing and works fine on my end. Can you check once in the settings of sum function whether it’s private or not?

@Mohit_Sharma : All the function I tried are “public”.

May you share a simple example ?
By this way I would find (I hope) a difference and by the way my mistake.

@Florian_Vigier: You can have a look at this, You can have a look at this, https://github.com/mongodb-developer/HelloRealmFunction

Hi,

Sorry for the delay, I had some exams to pass.
Thank you for the example.

I find what’s wrong on my app > 2 things
1/ For each call, it seems it’s necessary to say to the app it’s the currentUser who call the function even after a successful login
2/ I had to create a new User user = App.currentUser since I already have an user = AtomicReference which is not compatible.

So finally it work well.

Thank you so much for your help.