我在 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 ***');
} …Run Code Online (Sandbox Code Playgroud)