I'm using node to read from a table in SQLite and render to a webpage, but I can't seem to pass the values retrieved from the table on the server to client. I am able to log the values stored as JSON to the console, but having no luck passing them through. Here is my code:
//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();
}