gpt4 book ai didi

javascript - 服务器不断得到旧结果

转载 作者:行者123 更新时间:2023-11-30 14:50:41 25 4
gpt4 key购买 nike

添加用户后,新用户不会显示在列表中。一切只有在重新启动服务器后才能正常工作。看起来服务器正在从缓存中下载数据。也许我的代码没有写好。如何从数据库中获取当前数据?

// addFile

const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("./db/dbSqlite/app.db", sqlite3.OPEN_READWRITE);

const add = (user) => {
return new Promise((res, rej) => {
db.serialize(() => {
let status = false;
db.run(`INSERT INTO users(
login,
password,
) VALUES(?, ?)`, user, (err) => {
if (err) rej(status);
status = true;
res(status);
});
})
})
}
module.exports = add;

// getAll file

const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("./db/dbSqlite/app.db");

const getAll = new Promise((res, rej) => {
db.all(`SELECT * from users`, (err, row) => {
if (row === undefined || err) {
res("NO_TABLE_USERS");
} else {
const stringified = JSON.stringify(row)
res(JSON.parse(stringified));
}
});
})

module.exports = getAll

// route

router.post("/add", helper.isLoggedIn, helper.isAdmin, (req, res) => {
let msg = "User created successfully!";
user.add(Object.values(req.body)
.then((state) => {
if (!state) msg = "Name already used!";
user.getAll
.then((result) => res.render("users", {
name: req.user,
users: result,
msg: msg
}));
}));
});

最佳答案

getAll 应该是一个函数,就像您对 add 所做的一样。否则,getAll 将被解析一次:

// addFile

const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("./db/dbSqlite/app.db", sqlite3.OPEN_READWRITE);

const add = (user) => {
return new Promise((res, rej) => {
db.serialize(() => {
let status = false;
db.run(`INSERT INTO users(
login,
password,
) VALUES(?, ?)`, user, (err) => {
if (err) rej(status);
status = true;
res(status);
});
})
})
}
module.exports = add;

// getAll file

const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("./db/dbSqlite/app.db");

const getAll = () => {
return new Promise((res, rej) => {
db.all(`SELECT * from users`, (err, row) => {
if (row === undefined || err) {
res("NO_TABLE_USERS");
} else {
const stringified = JSON.stringify(row)
res(JSON.parse(stringified));
}
});
});
}

module.exports = getAll

// route

router.post("/add", helper.isLoggedIn, helper.isAdmin, (req, res) => {
let msg = "User created successfully!";
user.add(Object.values(req.body)
.then((state) => {
if (!state) msg = "Name already used!";
user.getAll()
.then((result) => res.render("users", {
name: req.user,
users: result,
msg: msg
}));
}));
});

关于javascript - 服务器不断得到旧结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48148732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com