Inf*_*911 7 javascript transactions mongoose mongodb node.js
我想利用 mongoose 的withTransaction帮助程序,特别是它能够自动重试瞬时事务错误。然而,助手似乎withTransaction无法返回数据,这对我来说是一个问题。
我的代码看起来像:
import { startSession } from 'mongoose';
async addItem(itemData) {
const session = await startSession();
session.startTransaction();
try {
const item = await new Item({ itemData }).save({ session });
// a bunch of other async operations...
await session.commitTransaction();
session.endSession();
return item;
} catch (error) {
await session.abortTransaction();
session.endSession();
throw error;
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能(1)使用withTransaction助手但仍然让这个函数像item当前一样返回 ,或者(2)通过除使用withTransaction.
| 归档时间: |
|
| 查看次数: |
3059 次 |
| 最近记录: |