nodejs vertica return query as json
I am working on a creating a nodejs express API querying a Vertica database using node-vertica. I can return the results but how do I convert/serialize the results in json format? Right now the results are being returned fields and rows. Also when I comment out my connection close the results of the query are no longer displayed in the browser. Here is the connection/query I have. Any help is greatly appreciated and if there is a better more supported module than node-vertica please let me know.
var express = require('express');
var router = express.Router();
Vertica = require('vertica');
var dbConfig = require('../secrets/dbconfig.js');
var config = {
ssl: 'optional',
interruptible: true,
host: 'xxxx',
user: dbConfig.iqi_user,
password: dbConfig.iqi_password,
database: dbConfig.iqi_DBname
};
try {
conn = Vertica.connect(config, (err, conn) => {
if (err) {
console.log('error');
} else {
//console.log(conn);
router.get('/', (req, res, next) => {
conn.query('SELECT * FROM LTE_USID_CQI_2018 LIMIT 5', (err, result) => {
if (err) throw 'hello' + err;
res.send(result);
});
});
//conn.disconnect();
}
});
}
catch (error) {
console.log("Error has been caught");
console.log(error);
}
module.exports = router;
sample output:
{
"fields":[
{
"name":"PERIOD_START_DATE",
"tableOID":982487324,
"tableFieldIndex":1,
"typeOID":10,
"type":"date",
"size":8,
"modifier":4294967295,
"formatCode":0
}
],
"rows":[
[
"2018-01-01",
"2018-01-01",
"Arkansas/Oklahoma",
"Arkansas",
14465,
-1.666162554,
-0.000055538882,
-0.00022931,
-0.001442423085,
-13.171986306,
-72.151515449,
-48.595225949,
394158,
396860,
2905,
396237,
397289,
397733.4534914,
2968127857,
590818.977,
102111.1,
854609.1,
133655950,
1606446,
27898.3106060606
]
],
"notices":[
],
"status":""
}
node.js api express vertica
add a comment |
I am working on a creating a nodejs express API querying a Vertica database using node-vertica. I can return the results but how do I convert/serialize the results in json format? Right now the results are being returned fields and rows. Also when I comment out my connection close the results of the query are no longer displayed in the browser. Here is the connection/query I have. Any help is greatly appreciated and if there is a better more supported module than node-vertica please let me know.
var express = require('express');
var router = express.Router();
Vertica = require('vertica');
var dbConfig = require('../secrets/dbconfig.js');
var config = {
ssl: 'optional',
interruptible: true,
host: 'xxxx',
user: dbConfig.iqi_user,
password: dbConfig.iqi_password,
database: dbConfig.iqi_DBname
};
try {
conn = Vertica.connect(config, (err, conn) => {
if (err) {
console.log('error');
} else {
//console.log(conn);
router.get('/', (req, res, next) => {
conn.query('SELECT * FROM LTE_USID_CQI_2018 LIMIT 5', (err, result) => {
if (err) throw 'hello' + err;
res.send(result);
});
});
//conn.disconnect();
}
});
}
catch (error) {
console.log("Error has been caught");
console.log(error);
}
module.exports = router;
sample output:
{
"fields":[
{
"name":"PERIOD_START_DATE",
"tableOID":982487324,
"tableFieldIndex":1,
"typeOID":10,
"type":"date",
"size":8,
"modifier":4294967295,
"formatCode":0
}
],
"rows":[
[
"2018-01-01",
"2018-01-01",
"Arkansas/Oklahoma",
"Arkansas",
14465,
-1.666162554,
-0.000055538882,
-0.00022931,
-0.001442423085,
-13.171986306,
-72.151515449,
-48.595225949,
394158,
396860,
2905,
396237,
397289,
397733.4534914,
2968127857,
590818.977,
102111.1,
854609.1,
133655950,
1606446,
27898.3106060606
]
],
"notices":[
],
"status":""
}
node.js api express vertica
Can you show us how the results is being displayed now?
– Bergur
Nov 26 '18 at 22:32
I added sample output in original post.
– rf guy
Nov 26 '18 at 22:37
1
fyi - There is a field name for each item in the row. I just pasted a sample.
– rf guy
Nov 26 '18 at 22:40
add a comment |
I am working on a creating a nodejs express API querying a Vertica database using node-vertica. I can return the results but how do I convert/serialize the results in json format? Right now the results are being returned fields and rows. Also when I comment out my connection close the results of the query are no longer displayed in the browser. Here is the connection/query I have. Any help is greatly appreciated and if there is a better more supported module than node-vertica please let me know.
var express = require('express');
var router = express.Router();
Vertica = require('vertica');
var dbConfig = require('../secrets/dbconfig.js');
var config = {
ssl: 'optional',
interruptible: true,
host: 'xxxx',
user: dbConfig.iqi_user,
password: dbConfig.iqi_password,
database: dbConfig.iqi_DBname
};
try {
conn = Vertica.connect(config, (err, conn) => {
if (err) {
console.log('error');
} else {
//console.log(conn);
router.get('/', (req, res, next) => {
conn.query('SELECT * FROM LTE_USID_CQI_2018 LIMIT 5', (err, result) => {
if (err) throw 'hello' + err;
res.send(result);
});
});
//conn.disconnect();
}
});
}
catch (error) {
console.log("Error has been caught");
console.log(error);
}
module.exports = router;
sample output:
{
"fields":[
{
"name":"PERIOD_START_DATE",
"tableOID":982487324,
"tableFieldIndex":1,
"typeOID":10,
"type":"date",
"size":8,
"modifier":4294967295,
"formatCode":0
}
],
"rows":[
[
"2018-01-01",
"2018-01-01",
"Arkansas/Oklahoma",
"Arkansas",
14465,
-1.666162554,
-0.000055538882,
-0.00022931,
-0.001442423085,
-13.171986306,
-72.151515449,
-48.595225949,
394158,
396860,
2905,
396237,
397289,
397733.4534914,
2968127857,
590818.977,
102111.1,
854609.1,
133655950,
1606446,
27898.3106060606
]
],
"notices":[
],
"status":""
}
node.js api express vertica
I am working on a creating a nodejs express API querying a Vertica database using node-vertica. I can return the results but how do I convert/serialize the results in json format? Right now the results are being returned fields and rows. Also when I comment out my connection close the results of the query are no longer displayed in the browser. Here is the connection/query I have. Any help is greatly appreciated and if there is a better more supported module than node-vertica please let me know.
var express = require('express');
var router = express.Router();
Vertica = require('vertica');
var dbConfig = require('../secrets/dbconfig.js');
var config = {
ssl: 'optional',
interruptible: true,
host: 'xxxx',
user: dbConfig.iqi_user,
password: dbConfig.iqi_password,
database: dbConfig.iqi_DBname
};
try {
conn = Vertica.connect(config, (err, conn) => {
if (err) {
console.log('error');
} else {
//console.log(conn);
router.get('/', (req, res, next) => {
conn.query('SELECT * FROM LTE_USID_CQI_2018 LIMIT 5', (err, result) => {
if (err) throw 'hello' + err;
res.send(result);
});
});
//conn.disconnect();
}
});
}
catch (error) {
console.log("Error has been caught");
console.log(error);
}
module.exports = router;
sample output:
{
"fields":[
{
"name":"PERIOD_START_DATE",
"tableOID":982487324,
"tableFieldIndex":1,
"typeOID":10,
"type":"date",
"size":8,
"modifier":4294967295,
"formatCode":0
}
],
"rows":[
[
"2018-01-01",
"2018-01-01",
"Arkansas/Oklahoma",
"Arkansas",
14465,
-1.666162554,
-0.000055538882,
-0.00022931,
-0.001442423085,
-13.171986306,
-72.151515449,
-48.595225949,
394158,
396860,
2905,
396237,
397289,
397733.4534914,
2968127857,
590818.977,
102111.1,
854609.1,
133655950,
1606446,
27898.3106060606
]
],
"notices":[
],
"status":""
}
node.js api express vertica
node.js api express vertica
edited Nov 26 '18 at 22:37
rf guy
asked Nov 26 '18 at 22:21
rf guyrf guy
7313
7313
Can you show us how the results is being displayed now?
– Bergur
Nov 26 '18 at 22:32
I added sample output in original post.
– rf guy
Nov 26 '18 at 22:37
1
fyi - There is a field name for each item in the row. I just pasted a sample.
– rf guy
Nov 26 '18 at 22:40
add a comment |
Can you show us how the results is being displayed now?
– Bergur
Nov 26 '18 at 22:32
I added sample output in original post.
– rf guy
Nov 26 '18 at 22:37
1
fyi - There is a field name for each item in the row. I just pasted a sample.
– rf guy
Nov 26 '18 at 22:40
Can you show us how the results is being displayed now?
– Bergur
Nov 26 '18 at 22:32
Can you show us how the results is being displayed now?
– Bergur
Nov 26 '18 at 22:32
I added sample output in original post.
– rf guy
Nov 26 '18 at 22:37
I added sample output in original post.
– rf guy
Nov 26 '18 at 22:37
1
1
fyi - There is a field name for each item in the row. I just pasted a sample.
– rf guy
Nov 26 '18 at 22:40
fyi - There is a field name for each item in the row. I just pasted a sample.
– rf guy
Nov 26 '18 at 22:40
add a comment |
1 Answer
1
active
oldest
votes
Hi rf guy and sorry for the late reply
Here is a snippet that transform the results to a standard json.
const res = {
"fields": [{
"name":" PERIOD_START_DATE"
}, {
"name": "STATE"
}, {
"name": "VALUE"
}],
"rows": [
[
"2018-01-01",
"Arkansas",
11111,
], [
"2019-01-01",
"Oklahomo",
22222,
]
]
}
function mapToJSON(dbResult) {
const fieldNames = dbResult.fields.map(field => field.name) // List of all field names
return dbResult.rows.map(row => {
return row.reduce((obj, item, index) => {
const header = fieldNames[index]
obj[header] = item
return obj
}, {})
})
}
const ans = mapToJSON(res)
console.log(ans)
In your code you could do something like
res.send(mapToJSON(result));
Or even better, since you're using Express
res.json(mapToJSON(result));
Best regards
Bergur
That works perfect! Thank you for you time and help. Do you mind up-voting my question? I am trying to build up my reputation. Thanks!
– rf guy
Nov 27 '18 at 18:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490008%2fnodejs-vertica-return-query-as-json%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Hi rf guy and sorry for the late reply
Here is a snippet that transform the results to a standard json.
const res = {
"fields": [{
"name":" PERIOD_START_DATE"
}, {
"name": "STATE"
}, {
"name": "VALUE"
}],
"rows": [
[
"2018-01-01",
"Arkansas",
11111,
], [
"2019-01-01",
"Oklahomo",
22222,
]
]
}
function mapToJSON(dbResult) {
const fieldNames = dbResult.fields.map(field => field.name) // List of all field names
return dbResult.rows.map(row => {
return row.reduce((obj, item, index) => {
const header = fieldNames[index]
obj[header] = item
return obj
}, {})
})
}
const ans = mapToJSON(res)
console.log(ans)
In your code you could do something like
res.send(mapToJSON(result));
Or even better, since you're using Express
res.json(mapToJSON(result));
Best regards
Bergur
That works perfect! Thank you for you time and help. Do you mind up-voting my question? I am trying to build up my reputation. Thanks!
– rf guy
Nov 27 '18 at 18:50
add a comment |
Hi rf guy and sorry for the late reply
Here is a snippet that transform the results to a standard json.
const res = {
"fields": [{
"name":" PERIOD_START_DATE"
}, {
"name": "STATE"
}, {
"name": "VALUE"
}],
"rows": [
[
"2018-01-01",
"Arkansas",
11111,
], [
"2019-01-01",
"Oklahomo",
22222,
]
]
}
function mapToJSON(dbResult) {
const fieldNames = dbResult.fields.map(field => field.name) // List of all field names
return dbResult.rows.map(row => {
return row.reduce((obj, item, index) => {
const header = fieldNames[index]
obj[header] = item
return obj
}, {})
})
}
const ans = mapToJSON(res)
console.log(ans)
In your code you could do something like
res.send(mapToJSON(result));
Or even better, since you're using Express
res.json(mapToJSON(result));
Best regards
Bergur
That works perfect! Thank you for you time and help. Do you mind up-voting my question? I am trying to build up my reputation. Thanks!
– rf guy
Nov 27 '18 at 18:50
add a comment |
Hi rf guy and sorry for the late reply
Here is a snippet that transform the results to a standard json.
const res = {
"fields": [{
"name":" PERIOD_START_DATE"
}, {
"name": "STATE"
}, {
"name": "VALUE"
}],
"rows": [
[
"2018-01-01",
"Arkansas",
11111,
], [
"2019-01-01",
"Oklahomo",
22222,
]
]
}
function mapToJSON(dbResult) {
const fieldNames = dbResult.fields.map(field => field.name) // List of all field names
return dbResult.rows.map(row => {
return row.reduce((obj, item, index) => {
const header = fieldNames[index]
obj[header] = item
return obj
}, {})
})
}
const ans = mapToJSON(res)
console.log(ans)
In your code you could do something like
res.send(mapToJSON(result));
Or even better, since you're using Express
res.json(mapToJSON(result));
Best regards
Bergur
Hi rf guy and sorry for the late reply
Here is a snippet that transform the results to a standard json.
const res = {
"fields": [{
"name":" PERIOD_START_DATE"
}, {
"name": "STATE"
}, {
"name": "VALUE"
}],
"rows": [
[
"2018-01-01",
"Arkansas",
11111,
], [
"2019-01-01",
"Oklahomo",
22222,
]
]
}
function mapToJSON(dbResult) {
const fieldNames = dbResult.fields.map(field => field.name) // List of all field names
return dbResult.rows.map(row => {
return row.reduce((obj, item, index) => {
const header = fieldNames[index]
obj[header] = item
return obj
}, {})
})
}
const ans = mapToJSON(res)
console.log(ans)
In your code you could do something like
res.send(mapToJSON(result));
Or even better, since you're using Express
res.json(mapToJSON(result));
Best regards
Bergur
const res = {
"fields": [{
"name":" PERIOD_START_DATE"
}, {
"name": "STATE"
}, {
"name": "VALUE"
}],
"rows": [
[
"2018-01-01",
"Arkansas",
11111,
], [
"2019-01-01",
"Oklahomo",
22222,
]
]
}
function mapToJSON(dbResult) {
const fieldNames = dbResult.fields.map(field => field.name) // List of all field names
return dbResult.rows.map(row => {
return row.reduce((obj, item, index) => {
const header = fieldNames[index]
obj[header] = item
return obj
}, {})
})
}
const ans = mapToJSON(res)
console.log(ans)
const res = {
"fields": [{
"name":" PERIOD_START_DATE"
}, {
"name": "STATE"
}, {
"name": "VALUE"
}],
"rows": [
[
"2018-01-01",
"Arkansas",
11111,
], [
"2019-01-01",
"Oklahomo",
22222,
]
]
}
function mapToJSON(dbResult) {
const fieldNames = dbResult.fields.map(field => field.name) // List of all field names
return dbResult.rows.map(row => {
return row.reduce((obj, item, index) => {
const header = fieldNames[index]
obj[header] = item
return obj
}, {})
})
}
const ans = mapToJSON(res)
console.log(ans)
answered Nov 27 '18 at 16:57
BergurBergur
1,053511
1,053511
That works perfect! Thank you for you time and help. Do you mind up-voting my question? I am trying to build up my reputation. Thanks!
– rf guy
Nov 27 '18 at 18:50
add a comment |
That works perfect! Thank you for you time and help. Do you mind up-voting my question? I am trying to build up my reputation. Thanks!
– rf guy
Nov 27 '18 at 18:50
That works perfect! Thank you for you time and help. Do you mind up-voting my question? I am trying to build up my reputation. Thanks!
– rf guy
Nov 27 '18 at 18:50
That works perfect! Thank you for you time and help. Do you mind up-voting my question? I am trying to build up my reputation. Thanks!
– rf guy
Nov 27 '18 at 18:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490008%2fnodejs-vertica-return-query-as-json%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show us how the results is being displayed now?
– Bergur
Nov 26 '18 at 22:32
I added sample output in original post.
– rf guy
Nov 26 '18 at 22:37
1
fyi - There is a field name for each item in the row. I just pasted a sample.
– rf guy
Nov 26 '18 at 22:40