0

I have a query that works in psql, but when I insert it into my REST service it doesnt work. Here is my code:

exports.getProjects = function(req, res) {
  'use strict';
  var query = client.query('select projects.project_id, array_to_string(array_agg(distinct project_subservices.subservice_id), ',') as subservices, array_to_string(array_agg(distinct project_certifications.certification_id), ',') as certifications from projects left outer join project_subservices on projects.project_id = project_subservices.project_id left outer join  project_certifications on projects.project_id = project_certifications.project_id group by projects.project_id');
  query.on('row', function(row, result) {
    result.addRow(row);
  });
  query.on('end', function(result) {
    var finalResult = {};
    finalResult.meta = {};
    finalResult.meta.count = result.rows.length;
    finalResult.projects = result.rows;
    res.json(finalResult);
  });
};

Here is the error I get:

/home/jason/Projects/Node/isx_server/node_modules/pg/lib/native/index.js:141
    this._sendQueryWithParams(query.text, query.values, query.singleRowMode);
         ^
Error: Values must be an array
    at Connection._pulseQueryQueue (/home/jason/Projects/Node/isx_server/node_modules/pg/lib/native/index.js:141:10)
    at Connection.connection.on.clientBuilder.port (/home/jason/Projects/Node/isx_server/node_modules/pg/lib/native/index.js:241:18)
    at Connection.EventEmitter.emit (events.js:93:17)

I can cut and paste this code into psql and it works find. What is the difference in the 2 environments that will cause the query not to work? How do I make this work? Thanks for the help.

2
  • 1
    Please show us your code! But anyway, your problem seems to be that you pass to sendQueryWithParams an argument (query.value), that it expects it to be an array, but it's not. Commented Jan 11, 2014 at 13:55
  • I added the full code. Commented Jan 11, 2014 at 16:30

1 Answer 1

1

You're not escaping those single quotes, so your js function things you're passing three variables instead of a single one…

If memory serves, js allows to use double quotes as string delimiters, so use that instead of single quotes seeing that you're not using double quotes in the query.

Else, add backslashes where needed to escape the single quotes.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.