const countResult = await db.collection('todos').count() const total = countResult.total console.log(total) //total 为 6
const total = await db.collection('todos').count().total
console.log(total)
//total 为 null
谢谢!
const countResult = await db.collection('todos').count() const total = countResult.total console.log(total) //total 为 6
const total = await db.collection('todos').count().total
console.log(total)
//total 为 null
谢谢!
|
1
试试加了括号的。
const total = (await db.collection('todos').count()).total 可以理解为,await 生效的那次调用,是最后一个点 |
|
fuzhuo233Sep 4, 2020
2
await 的优先级应该低于取 total 的值,而需要 await 先 resolve Promise,才能得到结果。话说优雅一点标准带语法糖不是用 const {total} = await db.collection('todos')
|
|
keepeyeSep 4, 2020
3
await 右边表达式的结果
.count() 本身是异步的,要等 await 成功之后才能获取到 total |
|
keepeyeSep 4, 2020
4
count().total 是错的 count()是个 Promise 对象
|
|
azh7138mSep 4, 2020
5
(await db.collection('todos').count()).total
await db.collection('todos').count().total 实际是 await undefined |