2014-01-11 108 views
0

我有一个在psql中工作的查询,但是当我将它插入到我的REST服务中时,它不起作用。这里是我的代码:查询在psql中工作,但不能与pg.js一起工作

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); 
    }); 
}; 

这是我得到的错误:

/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) 

我可以剪切此代码粘贴到psql和它的作品找到。 2个环境中会导致查询不起作用的区别是什么?我如何完成这项工作?谢谢您的帮助。

+1

请向我们展示您的代码!但无论如何,你的问题似乎是你传递给'sendQueryWithParams'的一个参数('query.value'),它期望它是一个数组,但事实并非如此。 –

+0

我添加了完整的代码。 – jhamm

回答

1

你不是逃避这些单引号,你传递的,而不是单一的一个三个变量让你的js函数的东西...

如果没记错,JS允许使用双引号作为字符串分隔符,所以使用而不是单引号看到你没有在查询中使用双引号。

否则,在需要转义单引号时加上反斜杠。

相关问题