Closed ats1999 closed 2 years ago
You aren't actually using the session. Do:
sesion.startTransaction();
const blog = new Blog({ title: "blog title" });
const byte = new Dsabytes({ topStory: "Not an ObjectId" });
await blog.save({ session: sesion });
await byte.save({ session: sesion }); // throw an error, because topStory should be an ObjectId
await sesion.commitTransaction();
sesion.endSession();
Gettig
MongoServerError: Transaction numbers are only allowed on a replica set member or mongos
Code
In the Dsabytes schema,I have defined
topStory
to be anObjectId
. When I run the above code withtopStory:"6167213f5003f3d062dd833f"
then it's working fine.But, when
await byte.save()
throws an error then ideallysesion.abortTransaction()
should remove the update caused byblog.save()
.Database before API call
Blogs
Dsabytes
DB after API call
Blogs
Dsabytes
So, when it throws an error then both collections should be empty at the end?