Reputation: 1043
I know that there are other questions just like this but my code just doesnt seems to work. Could you have a look at my code and tell me where i am wrong.
var mysql = require('mysql');
var client = mysql.createClient({
user: 'jed',
password: 'jed8703',
host: 'localhost',
database: 'jedtest'
});
//var query = client.query(
// 'INSERT INTO testtable '+
// 'SET testid = ?, name = ?, value = ?',
// [1, 'test', 'test']
//);
client.query(
'SELECT * FROM testtable',
function selectCb(err, results, fields) {
if (err) {
throw err;
}
console.log(results[0].Name);
for(var i in results)
{
(function(y)
{
setInterval(function() {
console.log(results[y].Name + 'value:' + results[y].Value );
}, 5000 );
})
}
}
);
client.end();
Upvotes: 1
Views: 16906
Reputation: 382686
You are not fullfilling y
variable, try replacing:
(function(y)
{
setInterval(function() {
console.log(results[y].Name + 'value:' + results[y].Value );
}, 5000 );
})
With:
(function(y)
{
setInterval(function() {
console.log(results[y].Name + 'value:' + results[y].Value );
}, 5000 );
})(i); // <------------------
Upvotes: 2
Reputation: 348992
Don't forget to invoke the function:
(function(y)
{
setInterval(function() {
console.log(results[y].Name + 'value:' + results[y].Value );
}, 5000 );
})(i); // <------- Added (i);
Note that your delay may not behave as expected. Currently, you're executing all methods after 5 seconds. Create a queue if you want to have a delay of 5 seconds between each call.
Upvotes: 6