Nodejs mongodb的Transaction API`withTransaction`始终返回null

编程入门 行业动态 更新时间:2024-10-10 21:21:37

Nodejs mongodb的<a href=https://www.elefans.com/category/jswz/34/1770405.html style=Transaction API`withTransaction`始终返回null"/>

Nodejs mongodb的Transaction API`withTransaction`始终返回null

我想从mongodb的withTransaction函数返回某些结果。但是,我似乎只返回了null。

官方文档中记录了应返还诺言。

.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
}) ()

编辑:

由于异步函数总是返回Promise。我用一个简单的字符串替换了new Promise(),但是结果却完全一样,这很令人困惑。
const res =   session.withTransaction(async function() {
  return `laughed`
})

我想从mongodb的withTransaction函数返回某些结果。但是,我似乎无法返回任何东西,除了null之外。官方文档中记录了...

回答如下:

官方文档中记录了应返还诺言。

更多推荐

Nodejs mongodb的Transaction API`withTransaction`始终返回null

本文发布于:2024-05-07 05:26:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Transaction   mongodb   Nodejs   null   withTransaction

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!