I am a long time PHP developer who decided to give node.js and express a try today. I am trying to find the best way to merge my results into a single object. I may be approaching this to much like a PHP developer and could use some assistance. Thanks in advance.
app.get('/api/posts', function(req, res) {
url_parts = url.parse(req.url, true)
query = url_parts.query
current_page = query.page || 1
items_per_page = 50
start_index = (current_page - 1) * items_per_page
max_page = 1000
var getPosts = {
db: function() {
var posts = {}
connection.query('SELECT COUNT(*) AS count FROM rss', function(err, rows1, fields) {
if (!err)
{
total_pages = Math.ceil(rows1[0].count / items_per_page)
if (start_index < rows1[0].count || start_index < max_page)
{
sql = 'SELECT id, title, image, width, height, url FROM rss ORDER BY date DESC LIMIT '+start_index+', '+items_per_page
connection.query(sql, function(err, rows2) {
if (!err)
{
for (var i in rows2)
{
comments = 'SELECT comment FROM comments WHERE section_id = '+rows2[i].id+' ORDER BY date DESC'
connection.query(comments, function(err2, rows3) {
//COMBINE RESULTS HERE
//rows2[i].comments = rows3
});
}
//res.json(rows2)
// DISPLAY RESULTS HERE
}
else
{
console.log(err)
}
});
}
}
else
{
console.log(err)
}
});
}
}
getPosts.db();
});