What should the <SERVICE_NAME> be set to for the context.services.get<SERVICE_NAME>) function call?

When calling the context.services.get(<SERVICE_NAME>) function, what does <SERVICE_NAME> get set with?

I’ve seen that it should be set to your cluster name. And that seems to work best, but still not useful. When doing:

const service = context.services.get("Cluster0");

service gets this as a return object:

{"version":1}

Then is using service to get a db instance, it doesn’t work. So this…

const db = service.db("prod");

creates db as {} - which is unusable.

If I try anything other than “Cluster0” as the <SERVICE_NAME>, then the service object is undefined.

I can’t find anything in the documentation on this. Does anyone have a read into this? Much appreciated.

  • k

If you go to “Linked Data Sources” in the UI then you will see the Service Name you have set your cluster(s) to and that is what is supposed to go there.

As for your other comments, does doing DB call like this work? https://www.mongodb.com/docs/atlas/app-services/functions/mongodb/#query-mongodb-atlas

We may intentionally not allow you to inspect the contents of the database object within the function

1 Like

Tyler, thanks for confirming that my context.services.get() call is working properly. I wasn’t aware, but it makes sense that the return object may not be entirely visible. It may be a permissions issue I’m having in terms of the user the trigger is running under - may not have access to my db. But I am issuing the db command like this:

// 2. Get a database & collection const db = mongodb.db("myDatabase")

and that has worked for me in the past. I’ll have to look into setting up and confirming the auth for the trigger.

I think you may have meant to post more code than that. If you want to test if it is just a permissions issue, you should be able to set your function to “Run as System User” in which case the permissions of the user are not evaluated.

Let me know if I can help in any way!
Tyler

1 Like

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