Retrieve array elements into a variable in custom functions in Realm


I am struggling to retrieve the elements of an array in the customer functions of Realm UI.

I have a simple collection and here is an example of a document

    "_id" : ObjectId("5f1db4f0a3f141fb2f1e6hfd"),
    "_partition" : "My Project",
    "online" : [ 
    "day" : "Monday",
    "minute" : 0,
    "hour" : 0

I want to retrieve the elements inside the ‘online’ array and later run another function over those Object ids that represent the ids of users in another collection.

I am simply doing the following:

exports = async function(){
  const cluster ="mongodb-atlas");
  const myCollection = cluster.db("myDB").collection("myCollection");
  const result = await availability.find({ day: "Monday", hour: 0, minute: 0 });
  //From here on, nothing is working properly

Until now all is good but after that, I am not able to retrieve the elements of the ‘online’ array.
I tried different methods, but everything is giving me “$undefined”: true …

What can I do to retrieve those 3 object ids and save them in a variable?

Please note that I am using the custom functions of the Realm UI. It is a must to use those functions as I am creating a Cron job using Realm.

Hi Mazen,

I don’t have enough context on where the bug might until I have the full function code. Can you copy the entire function here?

In the meantime, a couple of things:

  1. I’m not sure where your availability variable is defined.

  2. It seems like you might want to do something like:

exports = async function(arg) {
 const cluster ="mongodb-atlas");
  const myCollection = cluster.db("myDB").collection("myCollection");
const query = { "day": "Monday", "hour":0, "minute":0 };
const projection = { "online": 1 };

const result = await myCollection.find(query, projection).toArray()


This will return an array of arrays (all online arrays that fit the criteria in one array). Hope that’s helpful.

1 Like

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