读取从 "SELECT COUNT(*) FROM table" 返回的对象

Reading object returned from "SELECT COUNT(*) FROM table"

let rows = db.prepare("SELECT COUNT(*) FROM table").get();

Returns 对象

{ 'COUNT(*)': 2 }

我不知道怎么读

console.log(rows.COUNT(*));

Returns

SyntaxError: Unexpected token *

我发现解决方案是

Object.values(rows);

它将return一个长度为1的数组,其中只有数字

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

{ 'COUNT(*)': 2 }

因为COUNT(*)是一个键。您可以使用 Bracket notation

直接访问它
console.log('No of rows ', row['COUNT(*)']); //logs 2