0

我正在使用 Node.js 和 SQLite3 来与我的数据库通信。我可以成功添加新数据并读取现有数据。我的问题是,除非我重新启动 Node.js 服务器,否则我无法检索通过界面添加的数据。

我意识到数据库可能已被锁定,或者需要更改重新读取新数据的功能,但我终生无法弄清楚如何操作。

请原谅代码,它旨在跟踪随着时间的推移已挤出多少牛奶并绘制图表。

服务器.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)
    })

数据库.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

4

1 回答 1

0

答案其实很简单,我不敢相信我错过了。我只是将我的“lastAmountLeft”和“lastAmountRight”函数包装在 a 中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)

于 2019-06-02T20:59:50.163 回答