I am trying to connect to mongodb atlas from firebase functions like.
export default async () => {
try {
const url = 'mongodb+srv://foo:[email protected]/my-db?retryWrites=true';
const client = await MongoClient.connect(url);
client.dbName('my-db');
return client;
} catch (e) {
throw e;
}
}
However, I am getting this error:
{ "code": "ESERVFAIL", "errno": "ESERVFAIL", "syscall": "querySrv", "hostname": "_mongodb._tcp.foo-cluster.mongodb.net" }
- I made sure that my firebase plan is set to Blaze so I can connect to any client outside of google network.
- I whitelisted the functions' IP in mongodb atlas dashboard, I also added "connect from everywhere" just to make sure.
- I am using nodejs mongo driver version
^3.1.0-beta4
Any thoughts? Thanks.
retryWrites
option, and you probably don't want themy-db
on the end if you used the Atlas console to create users, because they should be in the default "admin" database. Alsoclient.db('my-db')
( which is another correction ) does not "persist". You want to return theclient
instance and select the "database" in different code. The error itself seems more indicative of a communication problem. Probably because you have not added the remote host to the whitelist. – Neil Lunn3.1.0-beta4
is actually marked asstable
onnpm
. It shouldn't be. Please also make sure you install3.0.8
explicitly until somebody fixes that. – Neil Lunnclient
context is better and then you simply select the.db()
from that. It's not really an "instance" anyhow, as nothing communicates with the server after connection until you call a collection method, or something else that does something. – Neil Lunn