我正在使用节点从SQLite中的表中读取并呈现到网页,但我似乎无法将从服务器上的表中检索到的值传递给客户端。我能够将存储为JSON的值记录到控制台,但没有运气通过它们。这是我的代码:
//Set up web server
const express = require('express')
const app = express()
//Set view engine and allow access to public/css
app.set('view engine', 'ejs')
app.use(express.static('public'));
//Start server
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
//Process get request and point to index.ejs
app.get('/', function(req, res) {
res.render('index', {
temps: getTemps()
});
});
function getTemps(){
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('fishtank.db');
var temps = "";
db.serialize(function() {
var rowset = db.each("SELECT id, datetime, temp FROM watertemp", function(err, row) {
temps = [{
id: row.id,
datetime: row.datetime,
temp: row.temp
}];
console.log(temps);
return temps;
});
});
//Close databse connection
db.close();
}