atlas mongodb serverless 和 aws lambda 连接问题

Lor*_*Vee 7 mongodb amazon-web-services aws-lambda

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

mongodb+srv://<username>:<password>@xyz.esxbh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
Run Code Online (Sandbox Code Playgroud)

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

{"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`","
Run Code Online (Sandbox Code Playgroud)

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

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

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

来自dns服务器的TXT条目记录显示:

"TXT    "authSource=admin&loadBalanced=true"
Run Code Online (Sandbox Code Playgroud)

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

产生错误的代码依赖于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)
        }
Run Code Online (Sandbox Code Playgroud)

看来与数据库的连接是好的:

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

} catch(err){
    console.log('error when connecting');
    return errorHandler(500, err);
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

小智 11

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

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

工作日期:2021 年 9 月 10 日