Javascript To Query influxDB

Hi, anyone able to assist with connecting and querying Influx DB using javascript? I have seen some tutorials out there, but when use an api to run the query I get an unhandled promise rejection warning:

(node:14356) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14356) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The code I am using is below.

const Influx = require(‘influx’);
const express = require(‘express’);
const path = require(‘path’);
const os = require(‘os’);
const bodyParser = require(‘body-parser’);
const app = express();
const influx = new Influx.InfluxDB(‘http://user:pass@datas:8086/db’);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));

app.use(express.static(path.join(__dirname, ‘public’)));
app.set(‘port’, 3000);

influx.getMeasurements()
.then(names => console.log(‘My measurement names are: ’ + names.join(’, ')))
.then(() => {
app.listen(app.get(‘port’), () => {
console.log(Listening on ${app.get('port')}.);
});
})
.catch(error => console.log({ error }));

app.get(‘/api/v1/usage’, (request, response) => {
influx.query(SELECT mean(*) FROM \"operatingsystem\" WHERE \"environment\" = \"ENV\" AND time >= (now() - 1m) GROUP BY time(1m), \"component\" fill(none) host = ${Influx.escape.stringLit(os.hostname())})
.then(result => response.status(200).json(result))
.catch(error => response.status(500).json({ error }));
});

Hi there,

Everything looks correct from what I can tell - where is the error being thrown exactly? On the request for ‘/api/v1/usage’?

Are your measurement names logging in the console successfully? (which would indicate the connection to the database was successful)

Another option would be to try simplifying your query (and then slowly increase the complexity) to ensure that it’s returning data successfully.

Good luck!

Hey, thanks for responding, the error is :

node:6100) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
at JSON.stringify ()
at stringify (c:\nodeJSCode\node_modules\express\lib\response.js:1119:12)
at ServerResponse.json (c:\nodeJSCode\node_modules\express\lib\response.js:260:14)
at influx.query.then.catch.error (c:\nodeJSCode\connectToInflux.js:35:44)
at
at process._tickCallback (internal/process/next_tick.js:160:7)
(node:6100) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6100) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

and yes, the error is thrown at the api section…

I think the issue may lie in this part of the error:

node:6100) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
at JSON.stringify ()

It looks like the result is coming back as a circular structure, and is unable to convert that to JSON. See this link for more info on that.

I would recommend delving into exactly what type of result you’re getting back from your InfluxDB query:

> influx.query(SELECT mean(*) FROM \"operatingsystem\" WHERE \"environment\" = \"ENV\" AND time >= (now() - 1m) GROUP BY time(1m), \"component\" fill(none) host = ${Influx.escape.stringLit(os.hostname())})
.then(result => response.status(200).json(result))

Hope that helps.