0

I have found other questions similar to mind, but my situation appears to be different. I have a query that gives me the expected result in pgAdmin, but when I try to use it in my Node application, I get a strange result.

Here is the query

select locationid, json_agg(json_build_object('dayofweek',dayofweek, 
   'start', case when interval_start is NULL then '0' else to_char(interval_start, 'HH12:MI:SS') end, 
   'interval_end', case when interval_end is NULL then '0' else to_char(interval_start,         'HH12:MI:SS') end)) as open_hours 
    from availability 
    group by locationid

When I run this in pgAdmin, I get what I expect pictured below.

[![enter image description here][1]][1]

When it run the same query in the application, I get

[
0|server  |   { locationid: '11', open_hours: [ [Object], [Object] ] },
0|server  |   { locationid: '9', open_hours: [ [Object] ] },
0|server  |   {
0|server  |     locationid: '15',
0|server  |     open_hours: [ [Object], [Object], [Object], [Object], [Object] ]
0|server  |   },
0|server  |   { locationid: '13', open_hours: [ [Object], [Object], [Object] ] },
0|server  |   {
0|server  |     locationid: '7',
0|server  |     open_hours: [ [Object], [Object], [Object], [Object], [Object] ]
0|server  |   }
0|server  | ]

The NodeJS code at this query is

db.any('select locationid, json_agg(json_build_object(\'dayofweek\',dayofweek, ' + 
    ' \'start\', ' + 
    'case  when interval_start is NULL then \'0\' ' +
    'else to_char(interval_start, \'HH12:MI:SS\') end, ' +  
    ' \'interval_end\', ' +  
    'case when interval_end is NULL then \'0\' ' +  
    'else to_char(interval_start, \'HH12:MI:SS\') end)) as open_hours ' + 
    'from availability ' +  
    'group by locationid')
    .then(function(data) {
         console.log(data)
         res.json(data)
    }).catch(function (err) {
         console.log(err);
    });

Has anyone run into this? It does look like I get an [Object] for each row matching the locationid in the data.

freqnseverity
  • 146
  • 1
  • 7
  • `[Object]` is just how `console.log` prints too-deeply-nested data into the terminal. Your code is working fine. Are you not getting the expected response at the client? – Bergi Feb 10 '23 at 00:05
  • You've missed to include the pgadmin output. – Bergi Feb 10 '23 at 00:05
  • This helps. I thought this was Node's way of telling me I have an error or it's just empty. I am working with the result in Swift on the client side, and the result doesn't match my struct properly, but I didn't work with that side much since I thought I was sending nonsense. – freqnseverity Feb 10 '23 at 00:08

0 Answers0