I’m using postgres and I try to make my request with array_agg in knex, but I have the error:
"message": "Expected 1 bindings, saw 0"
Does anyone know where this error may come from? And how can I fix it?
my request:
knex('user')
.leftJoin('user_has_restaurant','user_has_restaurant.user_id','user.id')
.leftJoin('restaurant','user_has_restaurant.restaurant_id',
'restaurant.id')
.select([
'user.id AS user_id',
'user.name AS user_name',
knex.raw(
'ARRAY_AGG(restaurant.id) as id',
'ARRAY_AGG(restaurant.name) as name',
'ARRAY_AGG(restaurant.description) as description',
'ARRAY_AGG(restaurant.website) as website',
'ARRAY_AGG(restaurant.created_at) as created_at',
'ARRAY_AGG(restaurant.updated_at) as updated_at')
])
.groupBy('user.id')
.whereIn('user.id',`${userId}`)
Of course my userId is a dynamic array like [1 , 2 , 3 … ]
2
Answers
try passing it like this
.whereIn('user.id',userId)
The problem is in your second parameter!
when you do whereIn – the second parameter needs to be array and not string,
Just change to .whereIn(‘user.id’,userId)