Nodejs mongodb 的 Transaction API `withTransaction` 总是返回 null

And*_* k. 5 transactions mongodb node.js

我想从 mongodbwithTransaction函数返回某些结果。但是,我似乎无法返回除 null 之外的任何内容。

官方文档中记录了应该返回承诺。

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.
Run Code Online (Sandbox Code Playgroud)

索引.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
}) ()
Run Code Online (Sandbox Code Playgroud)

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

const res =   session.withTransaction(async function() {
  return `laughed`
})
Run Code Online (Sandbox Code Playgroud)

D. *_* SM 6

官方文档中记录了应该返回承诺。

文件说(强调我的):

重要提示:此方法要求用户返回 Promise,所有不返回 Promise 的 lambda 都将导致未定义的行为。

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

当前的实现似乎将 commitTransaction 的结果作为 withTransaction 的返回值返回,并且该结果似乎是null在成功提交的情况下。

我相信您的应用程序向驱动程序提供的承诺的结果在这里被丢弃。

因此,驱动程序的行为似乎与记录的一致。

此行为更改是在此处请求的