我是一个长期的 PHP 开发人员,今天决定给 node.js 并表达一个尝试。我正在尝试找到将结果合并到单个对象中的最佳方法。我可能会像 PHP 开发人员一样接近这一点,并且可以使用一些帮助。提前致谢。
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();
});