Skip to content

array_to_json return type is string. It should be an array #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
laurentdebricon opened this issue Oct 11, 2012 · 6 comments
Closed

array_to_json return type is string. It should be an array #199

laurentdebricon opened this issue Oct 11, 2012 · 6 comments

Comments

@laurentdebricon
Copy link

array_to_json return type is string. It should be an array.
So now I have to do :

query.on('row', function(row) {
row.thumb = new Array(row.thumb);
[...]

@laurentdebricon
Copy link
Author

in fact, new Array(row.thumb) doesn't work ... i am struggling how to get my postgres array column into a javascript Array, with or without using array_to_json

@laurentdebricon
Copy link
Author

I've found this stll open issue : #10
and with the function parseArray provided in the thread, i can do what I want.

Funny part : a Postgres Array of Integer doesn't need this parseArray function.

@ryandesign
Copy link

I just spent some time realizing this as well. What I found out:

Set up a table:

CREATE TABLE IF NOT EXISTS arrtest (n integer, s varchar);
INSERT INTO arrtest VALUES (4, 'foo'), (5, 'bar'), (6, 'baz');

Program:

var pg = require('pg');
var conString = 'postgres://user:password@localhost/database';
pg.connect(conString, function(err, client) {
  client.query("SELECT 'hello' AS a,\
    ARRAY[1, 2, 3] AS b,\
    ARRAY['xx', 'yy', 'zz'] AS c,\
    ARRAY(SELECT n FROM arrtest) AS d,\
    ARRAY(SELECT s FROM arrtest) AS e;", function(err, result) {
    console.log(result.rows[0]);
    pg.end();
  });
});

Result:

{ a: 'hello',
  b: [ 1, 2, 3 ],
  c: [ 'xx', 'yy', 'zz' ],
  d: [ 4, 5, 6 ],
  e: '{foo,bar,baz}' }

As you can imagine, the result for e is surprising; everything else looks right.

@gkawamoto
Copy link

Well, I've been through this problem too. It appears that it's a problem interpreting varchar Arrays. A simple change in your select to

SELECT 'hello' AS a,
ARRAY[1, 2, 3] AS b,
ARRAY['xx', 'yy', 'zz'] AS c,
ARRAY(SELECT n FROM arrtest) AS d,
ARRAY(SELECT s::TEXT FROM arrtest) AS e;

would (temporarily) solve the problem.

@brianc
Copy link
Owner

brianc commented Oct 4, 2013

Interesting - I'll take a look. As an aside in your initial example you use array_to_json. That will return a type JSON which node-postgres will correctly parse and turn into a javascript array. A "postgres" array of varchar is causing the problem, not array_to_json correct? The initial example was a bit incomplete.

@brianc
Copy link
Owner

brianc commented Oct 20, 2014

I think this is fixed - a char array parser was added to https://github.com/brianc/node-pg-types/blob/master/lib/textParsers.js

@brianc brianc closed this as completed Oct 20, 2014
brianc added a commit that referenced this issue Oct 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants