context.services.get(<SERVICE_NAME>) returning {"version":1}

I am writing a function for an Atlas Trigger and I have decided to use Node.js runtime environment to write this function. To start I am trying to connect to the cluster using the following command: “context.services.get(<SERVICE_NAME>)”. However, this method is only returning “{“version”:1}”. So when I attempt to connect to the db with the result using context.services.get(<SERVICE_NAME>).db(‘db_name’), it returns an empty object {}.

I have updated the Service Name and the DB Name to match the one in our setup. Any idea on why I am unable to properly access the cluster?

While we’re here I also want to ask if any of this setup is necessary for my use case. I am writing this scheduled trigger to scan a collection for records with a Due Date that match the current time. Upon locating a document that matches the criteria, it will update the status.

Hey @Nicholas_Jurgens,

If I understood correctly, you are encountering an issue while executing the Atlas Function.

Could you confirm if you have everything defined in your Linked Data Source tab?
For example, I’ve got this information:

After saving the draft and deploying the changes, I executed the code below:

exports = async function () {
   const result = context.services.get("mongodb-atlas").db('test');
   return {result}
}

And it returns the following output:

> result: 
{
  "result": {
    "collection": {},
    "aggregate": {},
    "getCollectionNames": {}
  }
}
> result (JavaScript): 
EJSON.parse('{"result":{"collection":{},"aggregate":{},"getCollectionNames":{}}}')

Please try executing the above code and see if this resolves the issue.

Regards,
Kushagra

1 Like