1
正如標題所示,我無法從connection.query()
函數以外的MySQL響應中訪問對象。如何將MySQL JSON對象從connection.query()傳遞給res.render()
router.get('/', function(req, res) {
var testimonials;
// Running database queries
connection.query('SELECT * FROM testimonials', function(err, rows, fields) {
if (!err) {
testimonials = rows;
console.log(testimonials); // THIS WORKS
}
if (err) {
console.log('Error performing database query.');
}
});
res.render('index', {
title: 'Title',
description: 'Description in here.',
testimonials: testimonials // THIS RETURNS UNDEFINED
});
});
工程就像一個魅力。謝謝! – jacobwarduk
@jacobwarduk如果我的回答對你有用,請點擊upvote/downvote箭頭下方的'V'標記爲已接受。謝謝! :) – Buzinas
是的,那伎倆:) – jacobwarduk