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.