从Firebase函数连接到MongoDB Atlas [英] Connecting to MongoDB Atlas from firebase functions

查看:177
本文介绍了从Firebase函数连接到MongoDB Atlas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从类似Firebase的功能连接到mongodb地图集.

I am trying to connect to mongodb atlas from firebase functions like.

export default async () => {
  try {
    const url = 'mongodb+srv://foo:bar@foo-cluster.mongodb.net/my-db?retryWrites=true';
    const client = await MongoClient.connect(url);
    client.dbName('my-db');
    return client;
  } catch (e) {
    throw e;
  }
}

但是,我遇到此错误:

{ 代码":"ESERVFAIL", "errno":"ESERVFAIL", "syscall":"querySrv", 主机名":"_ mongodb._tcp.foo-cluster.mongodb.net" }

{ "code": "ESERVFAIL", "errno": "ESERVFAIL", "syscall": "querySrv", "hostname": "_mongodb._tcp.foo-cluster.mongodb.net" }

  1. 我确保将我的Firebase计划设置为Blaze,以便我可以连接到Google网络之外的任何客户端.
  2. 我在mongodb Atlas仪表板中将功能的IP列入了白名单,我还添加了从任何地方连接"以确保安全.
  3. 我正在使用nodejs mongo驱动程序版本^3.1.0-beta4

有什么想法吗?谢谢.

推荐答案

从Firebase Function连接到Atlas时需要注意的地方很少.以下是返回已连接的客户端实例以在FB函数中进一步使用的正确方法:

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
}

说明:

  1. 据报道,如果您使用的是 Spark 计划,则无法连接到Atlas.如果尚未升级,请确保升级到Blaze.

  1. 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字符串–从Firebase连接到Atlas时,不应使用缩短的url格式.由于某些原因,只有较旧的长url格式才能在firebase中可靠地工作.

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变量–您应该在导出范围之外定义client变量,然后仅在尚未分配连接的客户端实例的情况下在函数内为其分配该实例.这将防止在每次函数调用时重新连接客户端. 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.

这篇关于从Firebase函数连接到MongoDB Atlas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆