Thread: Return a table from a function

Return a table from a function

From
Simon Connah
Date:
Hi,

I've written a function that returns a table. It works fine and I get the expected results. The only problem is that in
Node.jswhich is the client the table data is a string rather than a list of variables. Is there some way to change the
returndata so it is still like a table but in something like JSON? Or even better is there a way to return it as the
individualrows so that each column can be easily accessed? I'm using pg-promise to access PostgreSQL from a Node.js web
application.

Simon.


Attachment

Re: Return a table from a function

From
Laurenz Albe
Date:
On Tue, 2021-04-06 at 16:52 +0000, Simon Connah wrote:
> I've written a function that returns a table. It works fine and I get the expected results.
>  The only problem is that in Node.js which is the client the table data is a string rather
>  than a list of variables. Is there some way to change the return data so it is still like
>  a table but in something like JSON? Or even better is there a way to return it as the
>  individual rows so that each column can be easily accessed? I'm using pg-promise to access
>  PostgreSQL from a Node.js web application.

It's a bit unclear what you are doing, but I guess you should run

  SELECT * FROM myfunction('arg');

rather than

  SELECT myfunction('arg');

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com




Re: Return a table from a function

From
Simon Connah
Date:
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Wednesday, April 7th, 2021 at 09:44, Laurenz Albe <laurenz.albe@cybertec.at> wrote:

> On Tue, 2021-04-06 at 16:52 +0000, Simon Connah wrote:
>

> > I've written a function that returns a table. It works fine and I get the expected results.
> >

> > The only problem is that in Node.js which is the client the table data is a string rather
> >

> > than a list of variables. Is there some way to change the return data so it is still like
> >

> > a table but in something like JSON? Or even better is there a way to return it as the
> >

> > individual rows so that each column can be easily accessed? I'm using pg-promise to access
> >

> > PostgreSQL from a Node.js web application.
>

> It's a bit unclear what you are doing, but I guess you should run
>

> SELECT * FROM myfunction('arg');
>

> rather than
>

> SELECT myfunction('arg');
>

> Yours,
>

> Laurenz Albe
>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
>

> Cybertec | https://www.cybertec-postgresql.com

That fixed it perfectly! Thank you.
Attachment