【问题标题】:atlas mongodb serverless and aws lambda connection issueatlas mongodb 无服务器和 aws lambda 连接问题
【发布时间】:2021-10-07 01:25:15
【问题描述】:

我在 atlas (https://www.mongodb.com/serverless) 上有一个无服务器数据库。我使用了 ATLAS 推荐的连接字符串:

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

但是,当我尝试创建记录时,我收到以下错误:

{"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`","

我认为连接字符串不正确,另一方面,服务器的 dns 条目确实回复了 2 个服务器。

我尝试删除 '+srv' 部分,但在这种情况下,mongoose 的保存函数只会永远挂起,使 lambda 函数超时。

我在谷歌上找不到任何类似的问题。

来自dns服务器的TXT入口记录显示:

"TXT    "authSource=admin&loadBalanced=true"

您如何配置无服务器数据库以使其正常工作?

产生错误的代码依赖于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)
        }

好像连接数据库没问题:

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

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

现在,看看源代码,其实没什么太复杂的:

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

我现在真的很难理解出了什么问题,因为 authSource 似乎存在于 TXT 记录中。

【问题讨论】:

  • 实际的连接字符串看起来不错,是哪一行代码产生了这个错误?
  • 从 TXT 记录中删除 loadBalanced=true。
  • 您好,感谢您的两个回答。我无法删除 loadBalanced=true,因为这是 atlas 提供的服务。重新生成错误的代码,这是一个简单的 mongoose save() 函数:try { const customer = new Customer(cust); console.log('新客户创建');常量返回客户 = 等待客户.save(); console.log(returnedCustomer);返回服务器响应(200,返回客户); } catch(err){ console.log(err);返回errorHandler(500, err) }
  • @ArbazSiddiqui 我认为你的解决方案是对的,问题是我不控制服务器的 dns

标签: mongodb amazon-web-services aws-lambda


【解决方案1】:

在 Nodejs 中将 mongoose 升级到对我有用的最新版本。

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

于 2021 年 9 月 10 日工作

【讨论】:

  • 是的,它运行良好。谢谢你的回答,我也遇到了同样的问题,你救了我。
  • 很高兴得知我帮助了你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 2018-09-28
  • 1970-01-01
相关资源
最近更新 更多