简体   繁体   English

为什么 mongoose abortTransaction() 不起作用?

[英]Why mongoose abortTransaction() not working?

  • MacOS: 10.15.5 macOS:10.15.5
  • NodeJS: 10.16.3节点JS:10.16.3
  • Mongoose: 5.8, 5.9 Mongoose:5.8、5.9
  • MongoDB: 4.0.3 MongoDB:4.0.3

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'" “电子邮件”:“test111@test111.com”

Why abortTransaction() does not working as expected?为什么 abortTransaction() 不能按预期工作? What I'm doing wrong?我做错了什么?

Pass the session to the DB methods involved in the transaction:将 session 传递给事务中涉及的 DB 方法:

 const user = await User.findOne({ email: 'test@test.com' }, null, { session })
 user.email = 'test111@test111.com'
 await user.save({ session })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM