简体   繁体   中英

Loopback API Connection to MongoDB Atlas

I created an API using Loopback, and had it working great with a MongoDB on LocalHost. I am now trying to switch it over to use MongoDB Atlas (online).

I ran lb datasource and filled everything in, and now this is my datasources.json file:

{
  "db": {
    "host": "fishapi-cxtvd.mongodb.net",
    "port": 0,
    "url": "mongodb+srv://admin:********@fishapi-cxtvd.mongodb.net/test?retryWrites=true&w=majority",
    "database": "admin",
    "password": "********",
    "name": "MyMongoDB",
    "user": "admin",
    "useNewUrlParser": true,
    "connector": "mongodb"
  }
}

I have also tried reformatting it (as suggested on a different stack overflow question), so it looks like this:

{
  "db": {
    "url": "mongodb+srv://admin:********@fishapi-cxtvd.mongodb.net/test?retryWrites=true&w=majority",
    "name": "FishAPI",
    "connector": "mongodb"
  }
}

This is the error I receive whenever I run it:

MongoParseError: Cannot create data source "db": Cannot initialize connector "mongodb": URI does not have hostname, domain name and tld

Does anyone have any suggestions for what I need to change to get this properly connected? Any help is appreciated as this is my first time using Loopback, and MongoDB!

Are you using the latest version of loopback-connector-mongodb ?

The error message suggests that the connector (or the underlying MongoDB client library) is not able to parse url . Perhaps the schema mongodb+srv:// is not supported by the version you are using?

As far as I can tell, support for mongodb+srv:// URLs was added relatively recently (in April 2019), see the pull request https://github.com/strongloop/loopback-connector-mongodb/pull/497 . The change was released in May as part of loopback-connector-mongodb@4.2.0 . Please upgrade your project to use the latest connector version.

Alternatively, you can try to use mongodb:// instead. According to this MongoDB blog post , mongodb+srv was added in version 3.6 to simplify the configuration of a seed list used to connect to a cluster of MongoDB servers. According to the post, it should be possible to replace mongodb+srv:// URL containing a single host with mongodb:// URL containing multiple hosts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM