I have next code:
import User from 'models/user'
const session = await User.startSession()
session.startTransaction()
try{
const user = await User.findOne({ email: 'test@test.com' })
user.email = 'test111@test111.com'
await user.save()
throw
}
catch(e){
await session.abortTransaction()
}
finally{
await session.endSession()
}
But in database I see:
"email": "test111@test111.com'"
Why abortTransaction() does not working as expected? What I'm doing wrong?
Pass the session to the DB methods involved in the transaction:
const user = await User.findOne({ email: 'test@test.com' }, null, { session })
user.email = 'test111@test111.com'
await user.save({ session })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.