Nodejs mongodb 的交易 API `withTransaction` 总是 return null

Nodejs mongodb's Transaction API `withTransaction` always return null

我想 return 来自 mongodb 的 withTransaction 函数的某些结果。但是,我似乎无法 return 除了 null 之外的任何东西。

官方文档中记录了应该 returned 的承诺。

https://mongodb.github.io/node-mongodb-native/3.3/api/ClientSession.html

IMPORTANT: This method requires the user to return a Promise, all lambdas that do not
return a Promise will result in undefined behavior.

index.js

const mongodb = require('mongodb');

(async() => {
  const client = await mongodb.connect(url);
  const db = await client.db("testt");
  const session = client.startSession()

  const res =   session.withTransaction(async function() {
      return new Promise((resolve, reject) => {
        if(true) {
          resolve('laughed')
        } else {
          reject(`not laughing`);
        }
    })
  })
  console.log('result here')   //result here
  console.log(res)             //Promise { <pending> }
  console.log(await res);      //null
}) ()

编辑: 由于异步函数总是 return Promise。我用一个简单的字符串替换了new Promise(),但结果完全一样,令人困惑。

const res =   session.withTransaction(async function() {
  return `laughed`
})

It's documented in the official documentation that a promise should be returned.

文档说(强调我的):

IMPORTANT: This method requires the user to return a Promise, all lambdas that do not return a Promise will result in undefined behavior.

没有人声称您从回调中 return 的承诺将 return 来自 withTransaction。我根本没有看到任何关于 withTransaction 的 return 值的描述。

当前实现似乎 return commitTransaction 的结果作为 withTransaction 的 return 值,如果提交成功,该结果似乎是 null

我认为您的应用程序给予驱动程序的承诺结果已被丢弃 here

因此,驱动程序的行为似乎符合记录。

此行为更改为 requested here