{"action":"created","issue":{"url":"https://api.github.com/repos/brianc/node-postgres/issues/831","labels_url":"https://api.github.com/repos/brianc/node-postgres/issues/831/labels{/name}","comments_url":"https://api.github.com/repos/brianc/node-postgres/issues/831/comments","events_url":"https://api.github.com/repos/brianc/node-postgres/issues/831/events","html_url":"https://github.com/brianc/node-postgres/issues/831","id":98791036,"number":831,"title":"pg does not seem to support select array_to_json","user":{"login":"tigernassau","id":1473665,"avatar_url":"https://avatars.githubusercontent.com/u/1473665?v=3","gravatar_id":"","url":"https://api.github.com/users/tigernassau","html_url":"https://github.com/tigernassau","followers_url":"https://api.github.com/users/tigernassau/followers","following_url":"https://api.github.com/users/tigernassau/following{/other_user}","gists_url":"https://api.github.com/users/tigernassau/gists{/gist_id}","starred_url":"https://api.github.com/users/tigernassau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tigernassau/subscriptions","organizations_url":"https://api.github.com/users/tigernassau/orgs","repos_url":"https://api.github.com/users/tigernassau/repos","events_url":"https://api.github.com/users/tigernassau/events{/privacy}","received_events_url":"https://api.github.com/users/tigernassau/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-08-03T16:44:19Z","updated_at":"2015-08-04T01:35:02Z","closed_at":null,"body":"trying to return an array of json back to express to send into view template \r\n\r\nclient.query('select array_to_json(array_agg(signups)) from signups;',function(err,data) {\r\n        if (err) {\r\n          console.log(\"select signups failed\");\r\n        } else {\r\n          console.log(\"signups\");\r\n          console.log(data);\r\n          console.log(\"using stringify\");\r\n          console.log(JSON.stringify(data));\r\n          return (JSON.stringify(data.rows)); \r\n\r\nselect runs fine from psql command line \r\nfrom nodejs pg -> returning undefined\r\nselect runs but instead of an array of json, it returns a bunch of meta data such as row count, .... \r\ntried it returning just data, JSON.stringify(data) and JSON.stringify(data.rows)\r\n\r\nexamples found all show results being returned from REST functions with res.JSON - but we are just trying to the json data back into a non-rest function.   Is node-pg supporting this postgres data function ?  "},"comment":{"url":"https://api.github.com/repos/brianc/node-postgres/issues/comments/127448893","html_url":"https://github.com/brianc/node-postgres/issues/831#issuecomment-127448893","issue_url":"https://api.github.com/repos/brianc/node-postgres/issues/831","id":127448893,"user":{"login":"tigernassau","id":1473665,"avatar_url":"https://avatars.githubusercontent.com/u/1473665?v=3","gravatar_id":"","url":"https://api.github.com/users/tigernassau","html_url":"https://github.com/tigernassau","followers_url":"https://api.github.com/users/tigernassau/followers","following_url":"https://api.github.com/users/tigernassau/following{/other_user}","gists_url":"https://api.github.com/users/tigernassau/gists{/gist_id}","starred_url":"https://api.github.com/users/tigernassau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tigernassau/subscriptions","organizations_url":"https://api.github.com/users/tigernassau/orgs","repos_url":"https://api.github.com/users/tigernassau/repos","events_url":"https://api.github.com/users/tigernassau/events{/privacy}","received_events_url":"https://api.github.com/users/tigernassau/received_events","type":"User","site_admin":false},"created_at":"2015-08-04T01:35:02Z","updated_at":"2015-08-04T01:35:02Z","body":"On 08/03/2015 05:39 PM, Sehrope Sarkuni wrote:\n>\n> You can use |json_agg| to do this directly. There's no need to use \n> |array_to_json(array_agg(...))|:\n>\n> |SELECT json_agg(signups) FROM signups|\n> |\n> |\n>\n> This would return a single row with a single column, \"json_agg\", that \n> you could access in the query result as |data.rows[0].json_agg|. Since \n> the datatype is json, the driver will convert it to a javascript \n> object which will be an array of the rows of your table with column \n> names as properties.\n>\n\nthks - getting close - now it might be more of an node/express question \nto get the right returned data but hope you can help so we can add this \nto a node-pg FAQ since it's a common operation\n\n// in model.js\nfunction signups_all() {\n   client.connect(....\n   client.query('select json_agg(signups) from signups;', function(err, \ndata) {\n     console.log(data.rows[0].json_agg); //=> yes, gives the correct \narray of signups\n     return (data.rows[0].json_agg);\n   });\n   });\n}\nexports.signups_all = signups_all;\n\nin server.js\napp.get(\"/signups\", function(req, res) {\n   var signups = model.signups_all(function(err,result){\n     if (err) {\n       console.log(\"error\");\n     } else {\n       signups = result;\n       res.render(\"signups\", {signups:signups));\n     }\n   });\n});\n===> hangs \"waiting for localhost\"\n\n\n\n> —\n> Reply to this email directly or view it on GitHub \n> <https://github.com/brianc/node-postgres/issues/831#issuecomment-127433734>.\n>\n\n\n-- \nTiger Nassau, Inc.\nwww.tigernassau.com\n\n"}}