I'm writing a basic Google Cloud Function that is going to query a MongoDB Cluster from MongoDB Atlas. I'm writing inside the Google Console and I was sure to add "mongodb": "^3.0.2" to the dependencies in the package.json file.
Here's the function (I replaced the valid password, etc. in the uri for security):
/**
* Responds to any HTTP request that can provide a "message" field in the
body.
*
* @param {!Object} req Cloud Function request context.
* @param {!Object} res Cloud Function response context.
*/
exports.myPackage = (req, res) => {
var MongoClient = require('mongodb').MongoClient;
var uri = "mongodb+srv://<USERNAME>:<PASSWORD>@<CLUSTER-NAME>-vtbhs.mongodb.net/test";
MongoClient.connect(uri, function(err, client) {
if (err) {
console.log('err');
console.log(err);
} else {
const collection = client.db("test").collection("devices");
}
});
res.status(200).send('Success');
}
I'm sure the driver is up to date, and I copied most of this code directly from the Atlas docs. I've whitelisted all IPs from Atlas for testing.
Every time the function runs, I get the following error in the connect callback:
"{ Error: querySrv ESERVFAIL _mongodb._tcp.<CLUSTER-NAME>-vtbhs.mongodb.net
at errnoException (dns.js:28:10)
at QueryReqWrap.onresolve [as oncomplete] (dns.js:219:19)
code: 'ESERVFAIL',
errno: 'ESERVFAIL',
syscall: 'querySrv',
hostname: '_mongodb._tcp.<CLUSTER-NAME>-vtbhs.mongodb.net' }"
I also previously got an error like:
URI does not have hostname, domain name and tld at module.exports
Although that doesn't seem to be popping up anymore since I adjusted my password inside of Mongo (there may have been a non-html-encoded char in it).
Thanks in advance for any help!
URI does not have hostname, domain name and tld at module.exports
came from the fact that I had a non-escaped @ symbol in my password. See this SO post for more details – Logan May