So say I’ve got a function:
function grabOldestJob() {
var client = mysql.createClient({
user: dbConfig['USER'],
password: dbConfig['PASS'],
});
client.query('USE '+dbConfig['DATABASE']);
client.query('SELECT url FROM '+dbConfig['JOB_TABLE']+' ORDER BY added ASC LIMIT 1 ',
function selectCb(err, results, fields, passed) {
if (err) {
throw err;
}
client.end();
fetchFeed(results[0]['url']);
}
);
}
What I need is the results[0][‘url’] buried in the inline function, so either I’d like to get that variable out of that function so I can use it to return the grabOldestJob function or pass another function into the inline function so I can use results[0][‘url’] as a parameter.
I’m very new to the concepts of node.js and would like to make my code as ‘proper’ as possible. This function is the first in a process, it pulls a url out of a database, passes that to get fetched from the remote server, the xml feed gets parsed and certain bits get stored in a database. I’m hoping using the ability of node to ‘run lots of things at once’ I’ll be able to fetch->parse->save many feeds at the same time. Any best practice tips for this would also be greatly appreciated.
Use callback functions for this.
Like the
client.query(query,callback)function does. You pass in the data and a callback function that gets called if the processing completed. Do the same with yourfetchFeed(url,callback)function.Have a look at: Understanding the node.js event loop