我正在使用Node.js和SQLite3与我的数据库对话。我可以成功添加新数据并读取现有数据。我的问题是,除非重新启动Node.js服务器,否则无法检索通过接口添加的数据。
我意识到数据库可能已锁定,或者需要更改重新读取新数据的功能,但是我无法终生确定如何做。
请原谅该代码,该代码旨在跟踪一段时间内表达的牛奶量并绘制图形。
server.js
var express = require('express')
var tempstats = require('./db.js')
var path = require('path')
var app = express()
// var db = require('./db.js')
var sqlite3 = require('sqlite3')
var db = new sqlite3.Database('web-app.db')
var publicPath = path.resolve(__dirname, "public");
app.use(express.static(publicPath));
// Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded());
// Parse JSON bodies (as sent by API clients)
app.use(express.json());
// All general page links should be listed below
app.get("/", function(request, response) {
response.sendFile("/index.html", {});
});
// Access the parse results as request.body
app.post('/', function(request, response) {
console.log(request.body.user.leftBrest);
console.log(request.body.user.rightBrest);
db.run('INSERT INTO milk VALUES (NULL, ?, ?, CURRENT_TIMESTAMP)', [request.body.user.leftBrest, request.body.user.rightBrest], function(err) {
if (err) {
console.log("There's another error!" + err.message);
} else {
console.log('Record successfully added with id: ' + this.lastID);
response.sendFile(__dirname + "/public/index.html");
}
});
});
// SQL data interaction functions from db.js
app.get('/left', function(req, res) {
res.send(tempstats.lastAmountLeft + '')
console.log(tempstats.lastAmountLeft + '')
})
app.get('/right', function(req, res) {
res.send(tempstats.lastAmountRight + '')
console.log(tempstats.lastAmountRight + '')
})
app.get('/selectedTemp', function(req, res) {
res.send(tempstats.selectedTemp + '')
})
// Json testing sendfile is different to sendFile
app.get('/json', function(req, res) {
res.sendfile('./test.json', {})
})
// 4xx Errors are served from here
// app.use(function(req, res) {
// res.status(404)
// res.render('404.html', {
// urlAttempted: req.url
// })
// })
// Server listen code
var port = 3000
app.listen(port, function() {
console.log('The server is listening on port ' + port)
})
db.js
lastAmountLeft和LastAmountRight需要更新,但不需要更新。 (我也在HTML页面中使用它们的值)
'use strict'
var sqlite3 = require('sqlite3')
// var db = new sqlite3.Database('web-app.db')
let db = new sqlite3.Database('web-app.db', (err) => {
if (err) {
return console.error(err.message);
}
console.log('Connected to the in-memory SQlite database.');
});
const tempstats = {}
db.each('SELECT * FROM milk WHERE rowid = 3', function(err, row) {
if (err) {
console.log('Oh no!' + err.message);
} else {
console.log('Row ID: ' + row._id + " shows the left breast had an expressed volume of: " + row.left + "ml")
tempstats.specificAmountLeft = row.left;
}
})
db.get('SELECT * FROM milk WHERE date order by date desc limit 1', function(err, row) {
if (err) {
console.log('Oh no!' + err.message);
return;
}
console.log(row)
console.log("The last expressed volume from the left breast was: " + row.left + "ml")
tempstats.lastAmountLeft = row.left;
})
db.get('SELECT * FROM milk WHERE date order by date desc limit 1', function(err, row) {
if (err) {
console.log('Oh no!' + err.message);
return;
}
console.log(row)
console.log("The last expressed volume from the right breast was: " + row.right + "ml")
tempstats.lastAmountRight = row.right;
})
module.exports = tempstats
// module.exports = db
答案 0 :(得分:0)
答案实际上很简单,我简直不敢错过。我只是将我的“ lastAmountLeft”和“ lastAmountRight”函数包装在setInterval
中,每隔X秒调用一次。现在,我的页面将更新为最新数据。
setInterval(function() {
db.get('SELECT * FROM milk WHERE date order by date desc limit 1', function(err, row) {
if (err) {
console.log('Oh no!' + err.message);
return;
}
console.log(row)
console.log("The last expressed volume from the right breast was: " + row.right + "ml")
tempstats.lastAmountRight = row.right;
})
}, 1000)