node.jsmongodbmongodb-querysails.jssails-mongo

Sails-mongo not working with MongoDB Atlas URI


I was trying with the below configs :

In datastores.js I tried

adapter: 'sails-mongo',
url: 'mongodb+srv://xyz@cluster0.uaxyh.mongodb.net/xyz?retryWrites=true&w=majority',
ssl: true

The above URL is for MongoDB Atlas

In model.js

id: { type: 'string', columnName: '_id' }

Did npm install sails-mongo

Still I am unable to connect to my "MongoDB"

I am using "sails": "^1.2.4" , "sails-mongo": "^1.2.0"

For your Information I would like to tell that I was able to connect to same instance using Mongoose adapter.

The error I am getting is as below -

A hook (`orm`) failed to load!
Could not tear down the ORM hook.  Error details: Error: Consistency violation: Attempting to tear 
down a datastore (`default`) which is not currently registered with this adapter.  This is usually 
due to a race condition in userland code (e.g. attempting to tear down the same ORM instance more 
than once), or it could be
due to a bug in this adapter.  (If you get stumped, reach out at http://sailsjs.com/support.)
Error: Consistency violation: Unexpected error creating db connection manager:
MongoError: failed to connect to server [cluster0.uzpih.mongodb.net:27017] on first connect 
[MongoError: getaddrinfo ENOTFOUND cluster0.uzpih.mongodb.net cluster0.uzpih.mongodb.net:27017]

Please, suggest me where I am doing wrong! Thanks in advance


Solution

  • You can find the Correct URL at your MongoDB atlas connection tab , where you can select nodejs driver version 2.2.12 or later

    The Following url worked for me :

    url: 'mongodb://USER:PASSWORD@DB01-shard-00-00-yxtfo.mongodb.net:27017,DB01-shard-00-01-yxtfo.mongodb.net:27017,DB01-shard-00-02-yxtfo.mongodb.net:27017/DB?ssl=true&replicaSet=DB01-shard-0&authSource=admin&retryWrites=true&w=majority'
    

    Replace the correct values with your DB configs and Enjoy.