
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 Vegetable 2020 年 9 月 4 日 试试加了括号的。 const total = (await db.collection('todos').count()).total 可以理解为,await 生效的那次调用,是最后一个点 |
2 fuzhuo233 2020 年 9 月 4 日 await 的优先级应该低于取 total 的值,而需要 await 先 resolve Promise,才能得到结果。话说优雅一点标准带语法糖不是用 const {total} = await db.collection('todos') |
3 keepeye 2020 年 9 月 4 日 await 右边表达式的结果 .count() 本身是异步的,要等 await 成功之后才能获取到 total |
4 keepeye 2020 年 9 月 4 日 count().total 是错的 count()是个 Promise 对象 |
5 azh7138m 2020 年 9 月 4 日 (await db.collection('todos').count()).total await db.collection('todos').count().total 实际是 await undefined |