简体   繁体   English

atlas mongodb serverless 和 aws lambda 连接问题

[英]atlas mongodb serverless and aws lambda connection issue

I have got a serverless db on atlas ( https://www.mongodb.com/serverless ).我在地图集上有一个无服务器数据库( https://www.mongodb.com/serverless )。 I used the connection string recommended by ATLAS:我使用了 ATLAS 推荐的连接字符串:

mongodb+srv://<username>:<password>@xyz.esxbh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

however as soon as i try to create a record, i get the following error:但是,一旦我尝试创建记录,就会收到以下错误:

{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongoParseError: Text record must only set `authSource` or `replicaSet`","reason":{"errorType":"MongoParseError","errorMessage":"Text record must only set `authSource` or `replicaSet`","name":"MongoParseError","stack":["MongoParseError: Text record must only set `authSource` or `replicaSet`","

I don't think that the connection string is correct, on the other hand the dns entry for the server does reply with 2 servers.我认为连接字符串不正确,另一方面,服务器的 dns 条目确实回复了 2 个服务器。

I tried dropping the '+srv' part, however in that case the save function from mongoose just hangs forever timing out the lambda function. I tried dropping the '+srv' part, however in that case the save function from mongoose just hangs forever timing out the lambda function.

I could not find any similar problem on google.我在谷歌上找不到任何类似的问题。

The TXT entry record from the dns server shows: dns服务器的TXT入口记录显示:

"TXT    "authSource=admin&loadBalanced=true"

How have you configured the serverless database to work?您如何配置无服务器数据库以使其正常工作?

The code that generates the error depends on mongoose and is as follows:产生错误的代码依赖于mongoose,如下:

        try {
          const customer = new Customer(cust);
          console.log('new cusotmer created');
          const returnedCustomer = await customer.save();
          console.log(returnedCustomer);
          return serverResponse(200, returnedCustomer);
        } catch(err){
          console.log(err);
          return errorHandler(500, err)
        }

It seems that the connection to the database is fine:似乎与数据库的连接很好:

try {
    await dbTools.connectMongoose();
    console.log('*** connected ***');

} catch(err){
    console.log('error when connecting');
    return errorHandler(500, err);
}

Now, looking at the source code, nothing really too complicated:现在,查看源代码,没有什么太复杂的:

if (Object.keys(record).some(key => key !== 'authSource' && key !== 'replicaSet')) {
  return callback(
    new MongoParseError('Text record must only set `authSource` or `replicaSet`')
  );
}

I am now really struggling to understand what's wrong as authSource seems to be present in the TXT record.我现在真的很难理解出了什么问题,因为 authSource 似乎存在于 TXT 记录中。

Upgrading mongoose to the latest version worked for me in Nodejs .Nodejs 中将 mongoose升级到最新版本对我有用。

  1. Remove " mongoose " from package.json.从 package.json 中删除“ mongoose ”。
  2. Reinstall "npm i mongoose"重新安装“npm i mongoose”
  3. "mongoose version 6.0.5 " should work. “猫鼬版本6.0.5 ”应该可以工作。

Worked on 10-Sep-2021于 2021 年 9 月 10 日工作

For anyone still experiencing this in 2022, here is an answer from a MongoDB employee (at the bottom).对于任何在 2022 年仍然遇到这种情况的人,这里是 MongoDB 员工(在底部)的回答。 It's a native Node.js driver issue (nothing to do with AWS lambda specifically), which will waterfall to all ODM implementations that implement the native driver.这是本机 Node.js 驱动程序问题(与 AWS lambda 无关),这将影响到所有实现本机驱动程序的 ODM 实施。

https://www.mongodb.com/community/forums/t/atlas-serverless-and-dns-txt-record/117967/8 https://www.mongodb.com/community/forums/t/atlas-serverless-and-dns-txt-record/117967/8

According to Zia Ullah's answer to this same question, this is fixed by Mongoose 6.0.5.根据 Zia Ullah 对同一问题的回答,这已由 ZCCADDEDB567ABAE643E15DCF0974E503Z 6.0.5 修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM