Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atlas mongodb serverless and aws lambda connection issue

I have got a serverless db on atlas (https://www.mongodb.com/serverless). I used the connection string recommended by ATLAS:

mongodb+srv://<username>:<password>@xyz.esxbh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

however as soon as i try to create a record, i get the following error:

{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongoParseError: Text record must only set `authSource` or `replicaSet`","reason":{"errorType":"MongoParseError","errorMessage":"Text record must only set `authSource` or `replicaSet`","name":"MongoParseError","stack":["MongoParseError: Text record must only set `authSource` or `replicaSet`","

I don't think that the connection string is correct, on the other hand the dns entry for the server does reply with 2 servers.

I tried dropping the '+srv' part, however in that case the save function from mongoose just hangs forever timing out the lambda function.

I could not find any similar problem on google.

The TXT entry record from the dns server shows:

"TXT    "authSource=admin&loadBalanced=true"

How have you configured the serverless database to work?

The code that generates the error depends on mongoose and is as follows:

        try {
          const customer = new Customer(cust);
          console.log('new cusotmer created');
          const returnedCustomer = await customer.save();
          console.log(returnedCustomer);
          return serverResponse(200, returnedCustomer);
        } catch(err){
          console.log(err);
          return errorHandler(500, err)
        }

It seems that the connection to the database is fine:

try {
    await dbTools.connectMongoose();
    console.log('*** connected ***');

} catch(err){
    console.log('error when connecting');
    return errorHandler(500, err);
}

Now, looking at the source code, nothing really too complicated:

if (Object.keys(record).some(key => key !== 'authSource' && key !== 'replicaSet')) {
  return callback(
    new MongoParseError('Text record must only set `authSource` or `replicaSet`')
  );
}

I am now really struggling to understand what's wrong as authSource seems to be present in the TXT record.

like image 295
LordVee Avatar asked Aug 01 '21 09:08

LordVee


People also ask

Why is MongoDB not connecting to Atlas?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.

Can I use MongoDB with AWS Lambda?

Use the following best practices to properly manage connections between AWS Lambda and Atlas: Define the client to the MongoDB server outside the AWS Lambda handler function . Don't define a new MongoClient object each time you invoke your function.

Does MongoDB Atlas run on AWS?

Global cloud document database service. This Quick Start deploys MongoDB Atlas on the Amazon Web Services (AWS) Cloud. MongoDB Atlas helps to ensure availability, scalability, and security compliance by using automation to maintain performance at scale as your applications evolve.

How do I manage connections between AWS Lambda and Atlas?

Use the following best practices to properly manage connections between AWS Lambda and Atlas: Define the client to the MongoDB server outside the AWS Lambda handler function. Don't define a new MongoClient object each time you invoke your function. Doing so causes the driver to create a new database connection with each function call.

How much does it cost to use MongoDB Atlas?

MongoDB Atlas can be used for FREE with a M0 sized cluster. Deploy MongoDB in minutes within the MongoDB Cloud. Learn more about the Atlas Free Tier cluster here. AWS Lambda is Amazon's serverless computing platform and is one of the leaders in the space.

How do I get a mongoclient in lambda?

// Get the MongoClient by calling await on the promise. // Because this is a promise, it will only resolve once. // Use the client to return the name of the connected database. Some serverless frameworks that run on top of Lambda allow for multiple logical functions to be packaged together in a single Lambda handler function.

How do I set up a cluster in MongoDB Atlas?

When you are signed up and logged into the MongoDB Atlas dashboard, the first thing we'll do is set up a new cluster. Click the Build a Cluster button to get started. From here, select the Shared Clusters option, which will have the free tier we want to use.


Video Answer


1 Answers

Upgrading mongoose to the latest version worked for me in Nodejs.

  1. Remove "mongoose" from package.json.
  2. Reinstall "npm i mongoose"
  3. "mongoose version 6.0.5" should work.

Worked on 10-Sep-2021

like image 156
Zia Ullah Avatar answered Oct 17 '22 15:10

Zia Ullah