I tried to declare variable as Object or as Array but I still got this message that variable “cannot accept a set”. Type Set is not supported by plv8 at this moment (02/2016). Funny thing is that I got this error even if function returned only 1 row!
The only remedy I have found so far in plv8 is to use plain “return” in this case – instead of “return_next”. Looks like with “return_next” PostgreSQL is afraid that there could be other rows coming. You can declare function with returning type for example “returns setof text” and give it several ways to end – one with plain “return” with variable. And if you need to return more rows anyway you can use JSON format for it. Of course if you have a reasonable amount of rows.