Anurag
Anurag

Reputation: 407

MongoDB atlas trigger getting error: TypeError: Cannot access member 'db' of undefined

I am a newbie trying MongoDB-atlas free tier. I have a cluster named - "mongoCluster. Under it, I have a database - "testdb" and under it, have a collection - "testcollection". This collection has documents. The inserts and read from my java app are working fine.

Now I am trying to create a new scheduled trigger in MongoDB atlas. But as I am running the following two lines, I am getting the following error. Same holds true for any other mongo query like delete, update and insert.

Code:

exports = function() {
  
    const collection=context.services.get("mongoCluster").db("testdb").collection("testcollection");
    collection.insertOne({"a": "b"});
   // const doc = collection.findOne();

};

Error:

> ran on Mon Nov 16 2020 18:52:52 GMT-0800 (Pacific Standard Time)
> took 272.591178ms
> error: 
TypeError: Cannot access member 'db' of undefined
> trace: 
TypeError: Cannot access member 'db' of undefined
    at exports (function.js:24:24)
    at apply (<native code>)
    at function_wrapper.js:3:1
    at <anonymous>:8:1

Looking at the error, either the service name is incorrect ("undefined error") OR I am missing some permissions on the service/database/collection ("anonymous:8:1 error").

I read somewhere in the MongoDB documentation, the service name = cluster name. Is there a way to see whats my service name? Have tried service name in lower case too, but no luck. Also tried same with fully qualified service name - "mongocluster.qeat9.mongodb.net", same error.

Or it is something else I am missing? Can someone please help here?

EDIT 1:

Adding a screenshot of the trigger function text which has the sample commented code. See the second line from the bottom which shows the syntax I have been using.

enter image description here

Thanks in Advance!

Upvotes: 12

Views: 6963

Answers (5)

stromyc
stromyc

Reputation: 659

If you are using the default code provided from Atlas within the Function Editor examine the following for the wrong serviceName variable

// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
  var serviceName = "mongodb_atlas";

The serviceName variable should have a hyphen not an underscore.

// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
  var serviceName = "mongodb-atlas";

Upvotes: 0

kristytoman
kristytoman

Reputation: 71

Before you run the trigger, check whether you linked the cluster in the form above the function. click the link button after you select the cluster

(I spent hours solving this so excuse the red colour and bold font.)

Upvotes: 4

franklyn seabra
franklyn seabra

Reputation: 21

First, make sure what trigger you want to use. refer docs: mongodb trigger-types

Then, you have to link the data source (click on the button) Link the data source image

After that, you have to use the cluster name instead of "mongodb-atlas" as said in the comments of a Scheduled Trigger See comments here

Upvotes: 2

Anton Plebanovich
Anton Plebanovich

Reputation: 1526

In my case, I had to use the actual cluster name instead of the mongodb-atlas

Upvotes: 2

haley
haley

Reputation: 1175

I linked an example of the page where you should be able to find your linked clusters ("Linked Data Sources" in the side-nav). Then you can use that service name in context.services.get("my-svc")...

enter image description here

Upvotes: 10

Related Questions