0
有人可以幫助我在所有查詢完成後才呈現頁面嗎?Node-mysql wordpress在所有查詢和循環完成後獲取帖子和標籤並呈現頁面
app.get("/", function(req, res) {
connection = mysql.createConnection(dbconfig);
connection.connect(function(err) { if(!err) {
//Get all posts
connection.query('SELECT * from wp_posts WHERE post_status = "publish" AND post_type = "html5-blank" ORDER BY post_date DESC', function(err, rows, fields) { if(!err) {
//Get tags
for (var i in rows) {
connection.query('SELECT slug FROM wp_terms INNER JOIN wp_term_taxonomy ON wp_term_taxonomy.term_id = wp_terms.term_id INNER JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id WHERE taxonomy = "post_tag" AND object_id =' + rows[i].ID, function(err, _rows, fields) { if(!err) {
var slugs = '';
for(var y in _rows) {
slugs += '#' + _rows[y].slug + ' ';
};
rows[i]['hashtags'] = slugs;
console.log(i) //Returns last index
if(i == rows.length) res.render('content', {_page: '', _posts: rows}); //Executes multiple times
}})
};
}})
}});
});
即使我檢查i的值,我也無法獲得變量中的新值。有人可以建議什麼是在nodejs和node-sql中進行這種調用的理想方式嗎?