Nodejs mongodb 的事务 API `withTransaction` 总是返回 null [英] Nodejs mongodb's Transaction API `withTransaction` always return null

查看:128
本文介绍了Nodejs mongodb 的事务 API `withTransaction` 总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

官方文档中记录了应该返回一个promise.

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

<块引用>

IMPORTANT:此方法要求用户返回一个 Promise,所有不返回的 lambda返回一个 Promise 将导致未定义的行为.

index.js

const mongodb = require('mongodb');(异步() => {const 客户端 = 等待 mongodb.connect(url);const db = await client.db("testt");const session = client.startSession()const res = session.withTransaction(async function() {返回新的承诺((解决,拒绝)=> {如果属实) {解决('笑')} 别的 {拒绝(`不笑`);}})})console.log('result here')//result 在这里console.log(res)//Promise { <pending>}控制台日志(等待资源);//空值}) ()

由于异步函数总是返回 Promise.我把new Promise() 换成了一个简单的字符串,结果却一模一样,令人困惑.

const res = session.withTransaction(async function() {返回`笑了`})

解决方案

官方文档中记录了应该返回一个promise.

文档说(强调我的):

<块引用>

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

没有声明您从回调返回的承诺将从 withTransaction 返回.我根本没有看到对 withTransaction 返回值的任何描述.

当前实现似乎将 commitTransaction 的结果作为 withTransaction 的返回值返回,如果成功提交,该结果似乎为 null.

我相信您的应用程序向驱动程序提供的承诺的结果会被丢弃 此处.

因此,驱动程序的行为似乎与文档一致.

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

I would like to return certain result from mongodb's withTransaction function. However, I can't seem to return anything except null from it.

It's documented in the official documentation that a promise should be 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
}) ()

Edit: Since async function always return Promise. I replaced the the new Promise() with a simple string, but the result is exactly the same, which is confusing.

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

解决方案

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

The documentation says (emphasis mine):

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

There is no claim that the promise which you return from your callback will be returned from withTransaction. I do not see any description of withTransaction's return value at all.

The current implementation appears to return the result of commitTransaction as the return value of withTransaction, and that result appears to be null in case of a successful commit.

I believe the result of the promise that your application is giving to the driver is discarded here.

So, the driver appears to behave as documented.

This behavior change was requested here.

这篇关于Nodejs mongodb 的事务 API `withTransaction` 总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆