0

Here i am sending sensor data to database using simple javascript (johnny five) using this code

var five = require("johnny-five");
var pg = require("pg");
var conString = "pg://admin:raja@localhost:5432/data";
var client = new pg.Client(conString);

function call(n)
{
  client.connect();
  client.query("INSERT INTO sensordata (event) VALUES (n);");
}


var board = new five.Board();
board.on("ready", function() {

  var sensor = new five.Sensor.Digital(2);

  sensor.on("change", function() {
    var n = this.value;
    call(n);
  });
});

My question is can i pass the streaming data variable n in the sensor call back function to call method.

2
  • I don't understand the question. What's a "streaming data variable"? Also, according to the Johnny-Five docs, value is not a function. Commented Mar 8, 2016 at 13:32
  • Sensor data to database Commented Mar 10, 2016 at 14:30

1 Answer 1

1

I think, what you're looking here is a simple query templating, supported by all sql clients:

client.query("INSERT INTO sensordata (event) VALUES ($1);", n);

This way you'll be able to pass the value of n variable to your SQL query.

Update: And you don't have to call client.connect() before every call to client.query. You should only call it once to establish DB connection, after that pg.Client will use it to send all subsequent requests.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.