0

I'm implementing a small webserver in Node.js and Express and I was wondering how can I access to data variable inside the database callback? This whole code is inside an express route function and the db is sqlite3

var data = {
    color: "blue",
}

db.all("SELECT * FROM connections WHERE ip='" + req.connection.remoteAddress + "' AND connection='connected'",function(err,rows){
    console.log(rows.length)
    if (rows.length > 0) {
        data["color"] = "green";
    }else {
        data["color"] = "red";
    }
});
Pablo
  • 69
  • 5
  • 2
    Exactly the way you are using it now. – Quentin Jan 22 '16 at 12:39
  • 1
    I guess this is probably a duplicate of http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call but the question is missing the actual problem (which is that you try to read the updated value before the callback has run). – Quentin Jan 22 '16 at 12:40
  • 1
    Slight aside - I don't "do" node.js much, but how secure is `req.connection.remoteAddress` against interference from clients? If there's any way they can spoof text into it, you're open to SQL Injection here. – James Thorpe Jan 22 '16 at 12:44
  • I hope it's only an internal connection variable... – Pablo Jan 22 '16 at 12:48
  • @Pablo, consider `req.connection.remoteAddress` as internal connection in the callback or something else? – zangw Jan 22 '16 at 13:19

0 Answers0