查询在PGAdmin中有效,但在服务器上失败(node \ localhost)

时间:2018-05-17 18:32:57

标签: javascript postgresql pg

我们正在使用节点服务器,我们使用pg模块来调用我们的postgres数据库。在pgadmin中进行查询时,我得到了所需的结果,但是当我们尝试在我们的服务器(js)上运行相同的查询时,它会抛出错误,任何人都知道是什么原因造成的,以及如何解决这个问题?

这是一个查询:

 SELECT DISTINCT waswindows.computername as servername, waswindows.appcode, ipaddress, result, WASInventorywindows.uploaddate
    FROM public.waswindows JOIN WASInventorywindows
    ON (waswindows.appcode = $1 OR waswindows.computername LIKE $2 OR WASInventorywindows.appcustodian LIKE $2)
    AND position(WASInventorywindows.servername in waswindows.computername) > 0
    AND result = 'FAILED' AND waswindows.uploaddate = (SELECT MAX(uploaddate) FROM waswindows)

这在PGAdmin中完美运行(用params替换1美元和2美元),并且在pgAdmin和Server中运行非常类似的查询,没有任何错误,但是这失败了。这是一个错误日志:

PARAMS ARE: Alex  AND   %Alex%
{ error: syntax error at or near "JOIN"
    at Connection.parseE (C:\Users\326009107.MAPLE\Desktop\Compliance\GHSRiskTSSCompliance\node_modules\pg\lib\connection.js:567:11)
    at Connection.parseMessage (C:\Users\326009107.MAPLE\Desktop\Compliance\GHSRiskTSSCompliance\node_modules\pg\lib\connection.js:391:17)
    at Socket.<anonymous> (C:\Users\326009107.MAPLE\Desktop\Compliance\GHSRiskTSSCompliance\node_modules\pg\lib\connection.js:129:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)
  name: 'error',
  length: 94,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '143',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'scan.l',
  line: '1086',
  routine: 'scanner_yyerror' }
C:\Users\326009107.MAPLE\Desktop\Compliance\GHSRiskTSSCompliance\API\WAS\getdata.js:44
                                set.push(results.rows);

我们尝试将名称更改为别名,重新排序项目,更改数据库字符串,但仍然没有。

1 个答案:

答案 0 :(得分:1)

好的,所以我找到了原因并将其发布在这里,以便那些在同一问题上遇到困难的人,在网络端查询字符串时,确保有空格,例如:

  "SELECT DISTINCT name, address, city" +
  "FROM public.city JOIN employees"...

错误,正确的版本将如下所示:

  "SELECT DISTINCT name, address, city" +
  " FROM public.city JOIN employees"...
   ^_________note this space_______