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" }
^3.1.0-beta4
Any thoughts? Thanks.
Firebase Hosting is a CDN for hosting static websites. So it is not possible to host an application like MongoDB server. You can't host MongoDB on any Firebase services. You have to deploy it somewhere else.
If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.
There are few caveats when connecting to Atlas from Firebase Function. Below is the correct way to return a connected client instance for further use in your FB function:
import { MongoClient } from 'mongodb'
const uri = 'mongodb://<USER>:<PASSWORD>@foo-shard-00-00-xxx.gcp.mongodb.net:27017,foo-shard-00-01-xxx.gcp.mongodb.net:27017,foo-shard-00-02-xxx.gcp.mongodb.net:27017/test?ssl=true&replicaSet=FOO-shard-0&authSource=admin&retryWrites=true'
let client
export default async () => {
if (client && client.isConnected()) {
console.log('DB CLIENT ALREADY CONNECTED')
} else try {
client = await MongoClient.connect(uri, { useNewUrlParser: true })
console.log('DB CLIENT RECONNECTED')
}
catch (e) {
throw e
}
return client
}
Explanation:
reportedly, you cannot connect to Atlas if you are on a Spark plan. Make sure you upgrade to Blaze if you didn't yet.
uri
string – You should not use the shortened url format when connecting to Atlas from Firebase. For some reason, only the older, long url format works reliably from firebase.
client
variable – You should define the client
variable outside the export scope, and then assign the connected client instance to it inside the function, only if it is not already assigned. This will prevent reconnecting the client on every function invocation. Firebase functions are stateless, but not entirely. they only get shut down after some period of inactivity. This means that the connection will persist for some time. From docs: If you declare a variable in global scope, its value can be reused in subsequent invocations without having to be recomputed.
To solve this issue, i did:
Outbound Networks Requests are free up to 5gb/month. So just enable billing and enjoy.
More info about billing here. https://firebase.google.com/pricing#blaze-calculator
In my case, the network access rules of my mongodb atlas cluster didn't allow firebase function to access the database. I had to allow access from anywhere to get it working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With