Skip to content

Commit 3d4a576

Browse files
committed
Fix serverless#793 fix outdated example
1 parent 5aaf492 commit 3d4a576

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

Diff for: aws-node-mongodb-atlas/handler.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ const mongoClusterName = '';
1010
const mongoUser = '';
1111
const mongoDbName = '';
1212
const mongoPass = '';
13+
const clusterName = "";
1314

14-
const mongoConnStr = `mongodb+srv://${mongoUser}:${mongoPass}@${mongoClusterName}-tdoka.mongodb.net/${mongoDbName}?retryWrites=true`;
15+
const mongoConnStr = `mongodb+srv://${mongoUser}:${mongoPass}@${mongoClusterName}/${mongoDbName}?retryWrites=true&w=majority&appName=${clusterName}`;
1516

1617
const getPetType = () => {
1718
const msNow = Date.now();
@@ -52,17 +53,17 @@ const performQuery = async () => {
5253
const app = express();
5354

5455
app.get('/hello', async function (req, res) {
55-
if (!client.isConnected()) {
56+
// if (!client.isConnected()) { // no longer existing for newer mongodb lib versions
5657
// Cold start or connection timed out. Create new connection.
5758
try {
58-
await createConn();
59+
await createConn(); // no-op if already connected
5960
} catch (e) {
6061
res.json({
6162
error: e.message,
6263
});
6364
return;
6465
}
65-
}
66+
// }
6667

6768
// Connection ready. Perform insert and return result.
6869
try {

Diff for: aws-node-mongodb-atlas/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"dependencies": {
1313
"express": "^4.16.4",
1414
"faker": "^4.1.0",
15-
"mongodb": "^3.1.13",
15+
"mongodb": "^6.8.0",
1616
"serverless-http": "^1.9.0"
1717
}
1818
}

Diff for: aws-node-mongodb-atlas/serverless.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ service: my-service # NOTE: update this with your service name
22

33
provider:
44
name: aws
5-
runtime: nodejs12.x
5+
runtime: nodejs20.x
66

77
functions:
88
hello:

0 commit comments

Comments
 (0)