Reputation: 1610
I'm using knex
and I would like to return only the number instead of RowDataPacket
, this is my code:
let totalUsers = await ctx.db('user')
.join('group', 'user.id', 'group.user_id')
.where('user.first_name', 'like', '%' + firstName + '%')
.count()
.first();
console.log(totalUsers);
essentially this returns:
RowDataPacket { 'count(*)': 1 }
I would like to get only 1
, how can I do this?
Upvotes: 0
Views: 404
Reputation: 18979
I am pretty sure you can use this to get the count value:
let count = totalUsers["count(*)"];
I also think the drivers for the different DBRMSes will return different names for the count variables. For SQL Server, for instance, the count field didn't have a proper name when I tried, just ''
. So to make it work in all SQL dialects I would give the count an alias in the SQL query, like so:
.count('* AS c')
You can then access it like this:
let count = totalUsers["c"];
Upvotes: 1