gpt4 book ai didi

bookshelf.js - 使用 knex.js/bookshelf.js 获取计数结果

转载 作者:行者123 更新时间:2023-12-03 22:07:29 24 4
gpt4 key购买 nike

我正在尝试使用 knex 进行简单的计数(因为它似乎是 to not be supported by bookshelf yet )。以下代码正在运行:

bookshelf.knex('hosts').count('id').then(function(total) {
res.send({
meta: {
total: total[0]['count(`id`)']
}
});
});

我必须做 total[0]['count('id')'] 对我来说似乎很奇怪以获得实际结果。我在这里做事吗?

谢谢!

最佳答案

knex.js 的所有结果都是数组。查询可能会成功并且只返回 0 个结果。

此外,您可以直接在列名称(或 count() 调用)中为列设置别名。像这样:

  bookshelf.knex('hosts').count('id as CNT').then(function(total) {
res.send({
meta: {
total: total[0].CNT
}
});
});

仍然需要获取第一个元素,但您可以将该列作为普通 JSON 属性进行引用。

关于bookshelf.js - 使用 knex.js/bookshelf.js 获取计数结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21979388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com