And*_* k. 5 transactions mongodb node.js
我想从 mongodbwithTransaction函数返回某些结果。但是,我似乎无法返回除 null 之外的任何内容。
官方文档中记录了应该返回承诺。
https://mongodb.github.io/node-mongodb-native/3.3/api/ClientSession.html
Run Code Online (Sandbox Code Playgroud)IMPORTANT: This method requires the user to return a Promise, all lambdas that do not return a Promise will result in undefined behavior.
索引.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)
| 归档时间: |
|
| 查看次数: |
2818 次 |
| 最近记录: |