1

I have a simple SELECT query that is returning an unusable result. I am using pg-promise in node.js

[
  {
    "function_name": "(f10d1988-4db5-49de-97ab-0c8b15bedfa7,image.jpg,Image)"
  },
  {
    "function_name": "(f10d1988-4db5-49de-97ab-0c8b15bedfa7,image2.jpg,Image 2)"
  }
]

but I was expecting a basic json structure like

[
  {
    id: '',
    title: '',
    image: ''
  },
  {...etc}
]

Why is it doing this? How do I get a normalized result?

My query looks like the below:

CREATE OR REPLACE FUNCTION get_photos(
  title_param     TEXT
)
RETURNS TABLE(
  id         UUID,
  image      varchar(200),
  title      varchar(200) 
) AS
$func$
BEGIN
  RETURN QUERY SELECT
    i.id,
    i.image,
    i.title
  FROM images AS i
  WHERE i.title = title_param;
END;
$func$ LANGUAGE PLPGSQL;

Here is my db conenctor setup, almost all defaults.

require('dotenv').config();
const Promise = require('bluebird');
const pg = require('pg-promise')({
  promiseLib: Promise
});

const config = {
  user: process.env.USER,
  host: process.env.HOST,
  database: process.env.DATABASE,
  password: process.env.PASSWORD
};

const db = pg(config);

export default db;

Here is the express endpoint that is calling the function:

export const getData = async (req, res) => {
  const { title } = req.query;
  let data;
  try {
    data = await db.many('SELECT function_name($1)', [title]);
  } catch (err) {
    data = err;
  }
  res.send(data);
};

EDIT

I ran the query manually instead of through a function and the data returned correctly which means that there is an issue with my TABLE() return. What could possibly cause this issue?

images = await db.many(`
  SELECT
    p.id,
    p.img,
    p.type,
    p.title
  FROM photos p
  WHERE p.type = '${type}';
`, [type]);
3
  • Kindly post your js code, where you are catching the results. Commented Dec 24, 2019 at 5:50
  • @PrabhjotSinghKainth I added the db js and endpoint, mostly defaults Commented Dec 24, 2019 at 6:14
  • Just try res.send(data[0]); codota.com/code/javascript/functions/pg/Client/query Commented Dec 24, 2019 at 7:05

2 Answers 2

3

Because the function is defined as returning a table, you need to use it like a table:

SELECT * FROM function_name($1)
Sign up to request clarification or add additional context in comments.

Comments

1

Use func as the query method:

data = await db.func('function_name', [title]);

It assumes you return a table, and so will work for you by default.

And for stored procedures, there's proc method.


Also, your parameter formatting for the images query is wrong, see Named Parameters:

IMPORTANT: Never use the reserved ${} syntax inside ES6 template strings ...

3 Comments

I tried the above but received the following error error: function function_name($1)(unknown) does not exist but keeping the function as db.many(SELECT * FROM function_name(...etc)) works fine also, interesting that ${} shouldn't be used but worked in this simple case, still good to see the best practice as a reference
@Brandon It looks like you didn't pass it the expected parameters correctly, or there is no function_name function defined. You should include the code that you actually executed here. b.t.w. I am the author of pg-promise, and the solution I gave is the right way to do it.
I tried again, the issue is that I kepy function_name($1) when using db.func instead of just function_name. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.