Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c8a8a35

Browse files
committed
Update error handling
1 parent 8e478ef commit c8a8a35

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

‎transaction.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function main() {
99
* See http://bit.ly/NodeDocs_lauren for more details
1010
*/
1111
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
12-
12+
1313
/**
1414
* The Mongo Client you will use to interact with your database
1515
* See bit.ly/Node_MongoClient for more details
@@ -77,7 +77,7 @@ async function createReservation(client, userEmail, nameOfListing, reservationDa
7777
try {
7878
// Step 3: Use withTransaction to start a transaction, execute the callback, and commit (or abort on error)
7979
// Note: The callback for withTransaction MUST be async and/or return a Promise.
80-
// See http://bit.ly/Node_withTransaction for the withTransaction() docs
80+
// See http://bit.ly/Node_withTransaction for the withTransaction() docs
8181
const transactionResults = await session.withTransaction(async () => {
8282

8383
// Important:: You must pass the session to each of the operations
@@ -111,13 +111,13 @@ async function createReservation(client, userEmail, nameOfListing, reservationDa
111111

112112
}, transactionOptions);
113113

114-
// TODO: I couldn't find documentation that explicitly said that the transactionResults will be undefined if the transaction is
115-
// unsuccessful, but that seems to be the case. Can you confirm this is true?
116114
if (transactionResults) {
117115
console.log("The reservation was successfully created.");
118116
} else {
119-
console.log("An error occurred and the reservation could not be created.");
117+
console.log("The transaction was intentionally aborted.");
120118
}
119+
} catch(e){
120+
console.log("The transaction was aborted due to an unexpected error: " + e);
121121
} finally {
122122
// Step 4: End the session
123123
await session.endSession();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /