Search code examples
node.jsmongodbloopbackjs

Loopback API connecting to MongoDB Atlas, fails


Looks like there are about 5 of these types of questions, all unanswered, maybe someone who's done it will have some time to share a solution.

I've got a loopback API app running locally, and it connects fine locally to mongoDB - I've got "loopback-connector-mongodb": "^3.3.1" installed, and as mentioned - it all works fine

So here is the progression I went thru (As I'm unsure which driver is being used I tried both syntax, syntaxi? syntaxes?)

The local connection works

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "localhost",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "",
    "password": "",
}

My first attempt at connecting to ALAS. This SEEMED to work, but right after the feedback it errors.

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "mongodb://adminUser:pwd@cluster0-shard-00-00-xxx.mongodb.net:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "",
    "password": ""
}

Resulted in: Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer

c:\loopback-test\node_modules\mongodb\lib\replset.js:345 process.nextTick(function() { throw err; }) ^ MongoError: database names cannot contain the character '/'

I figured it didn't like '/test...', So I removed it

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "mongodb://adminUser:pwd@cluster0-shard-00-00-xxx.mongodb.net:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "",
    "password": ""
}

Resulted in: Error: Cannot create data source "loopback-test": Cannot initialize connector "mongodb": double colon in host identifier

    "loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "cluster0-shard-00-00-xxx.mongodb.net:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "adminUser",
    "password": "pwd"
}

Still Resulted in: Error: Cannot create data source "loopback-test": Cannot initialize connector "mongodb": double colon in host identifier

So by now Im starting to think Im using the 3.6 driver, but I want to try one more And I almost thought I had it - as it took a few seconds after the "Browse your REST API..." message, for the error to pop...but it did.

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "cluster0-shard-00-00-xxx.mongodb.net,cluster0-shard-00-01-xxx.mongodb.net,cluster0-shard-00-02-xxx.mongodb.net",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "adminUser",
    "password": "pwd"
}

Still Resulted in:

Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer Connection fails: MongoError: no mongos proxy available It will be retried for the next request.

c:\loopback-test\node_modules\mongodb\lib\mongo_client.js:421 throw err ^ MongoError: no mongos proxy available

So now IM onto the 3.6 driver syntax

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "mongodb+srv://adminUser:pwd@cluster0-xxx.mongodb.net",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "",
    "password": ""
}

Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer Connection fails: MongoError: failed to connect to server [cluster0-xxx.mongodb.net:27017] on first connect [MongoError: getaddrinfo ENOTFOUND cluster0-xxx.mongodb.net cluster0-xxx.mongodb.net:27017] It will be retried for the next request.

And lastly - I tried

"loopback-test": {
    "connector": "mongodb",
    "name": "loopback-test",
    "host": "cluster0-xxx.mongodb.net",
    "port": 27017,
    "url": "",
    "database": "test",
    "user": "adminUser",
    "password": "pwd"
}

Solution

  • To clarify, the problem was caused by MongoDB Connection String URI being inserted into the host parameter instead of the url parameter.

    The host parameter should only accept MongoDB server address, which can either be a hostname, IP address or UNIX domain socket.

    You can find more information about the parameters on Loopback/MongoDB Connection Properties.