从Google Cloud Functions控制台访问MongoDB Atlas集群
我正在编写一个基本的Google Cloud Function,它将从MongoDB Atlas查询MongoDB集群.我正在Google控制台内编写程序,并确保将package.json文件中的依赖项添加"mongodb":"^ 3.0.2".
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.
以下是功能(为了安全起见,我替换了uri中的有效密码等):
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');
}
我确定驱动程序是最新的,并且我直接从Atlas文档复制了大部分代码.我已将Atlas的所有IP列入白名单以进行测试.
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.
函数每次运行时,在connect回调中都会出现以下错误:
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
尽管自从我在Mongo内调整了密码以来,这种现象似乎没有再出现(其中可能包含非html编码的字符).
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).
在此先感谢您的帮助!
我遇到了完全相同的问题.运行firebase deploy
后,MongoDB Atlas和mLab连接均失败,但使用firebase serve
在本地工作.
I had the exact same issue. It was failing for both MongoDB Atlas and mLab connections after running firebase deploy
, but working locally using firebase serve
.
我相信有两个问题:
- 免费的Spark计划禁用了出站网络.为此,您必须升级到Blaze计划(即付即用).我只是切换了等级,现在就可以使用了.
Spark计划仅允许Google拥有的出站网络请求 服务.配额内允许入站调用请求.在 在Blaze计划中,Cloud Functions提供了永久的免费套餐.这 前2,000,000次调用,400,000 GB-秒,200,000 CPU-秒和5 GB 的互联网出口流量每月免费提供.你是 仅在免费分配后使用时付费.定价依据 调用总数和计算时间.计算时间为 变量基于为一个服务器配置的内存和CPU数量 功能.每天和100秒内还会强制使用限制 配额.有关更多信息,请参阅云功能定价.
The Spark plan allows outbound network requests only to Google-owned services. Inbound invocation requests are allowed within the quota. On the Blaze plan, Cloud Functions provides a perpetual free tier. The first 2,000,000 invocations, 400,000 GB-sec, 200,000 CPU-sec, and 5 GB of Internet egress traffic is provided for free each month. You are only charged on usage past this free allotment. Pricing is based on total number of invocations, and compute time. Compute time is variable based on the amount of memory and CPU provisioned for a function. Usage limits are also enforced through daily and 100s quotas. For more information, see Cloud Functions Pricing.
https://firebase.google.com/pricing/
您必须提供信用卡,但是只要您保持在其数据配额之内,就可以免收费用.我将尝试一下,看看效果如何.
You have to give a credit card, but as long as you remain under their data quota you don't get charged, apparently. I will try it out and see how it goes.
- 升级后,您的MongoDB Atlas连接可能仍会失败(而mLab字符串可以工作).这是因为您的查询URI使用的是Mongo 3.6 SRV地址,而不是Mongo 3.4驱动程序的"mongodb://"协议.尝试切换驱动程序版本,然后查看它是否有效.
- After upgrading, your MongoDB Atlas connection might still be failing (while an mLab string would work). This is because your query URI is using the Mongo 3.6 SRV address, rather than the Mongo 3.4 driver's "mongodb://" protocol. Try switching the driver version and see if it works.