gpt4 book ai didi

mysql - Node/Express + MySQL : Inserts not displaying instantly

转载 作者:行者123 更新时间:2023-11-29 11:13:24 25 4
gpt4 key购买 nike

我有一个名为#add_blog_post 的表单,其中包含操作“/mysql_test/add_blog_post”和“POST”方法

Jade 标记:

form#add_blog_post(action="/mysql_test/add_blog_post" method="POST")

此表单在我的 app.js 中执行以下代码:

app.post('/mysql_test/add_blog_post', function(req, res) {
var author = req.body.author;
var date = req.body.date;
var title = req.body.title;
var body = req.body.body;
var blog_insert_query = "insert into 332project.blog(author,date,title,body) values(";
blog_insert_query += ("'"+author+"'"+","); blog_insert_query += ("'"+date+"'"+","); blog_insert_query += ("'"+title+"'"+","); blog_insert_query += ("'"+body+"'"+")");

var connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS
});
connection.connect(function(err) { /*error?*/ });

var result;
var query = connection.query(blog_insert_query, function(err, result) {
res.redirect('/mysql_test');
});
});

博客文章插入工作正常,但网站需要一段时间才能从/mysql_test 上的 select 语句显示插入。

这是我的路线:

var express = require('express');
var router = express.Router();
var db_calls = require('../db.js');

var connection = db_calls.connect();
connection.connect(function(err) { /*error?*/ });

var result;
var query = connection.query("select * from 332project.blog order by id desc", function(err, rows, fields) {
connection.end();
if (!err) {
result = rows;
}
});

router.get('/', function(req, res, next) {
res.render('mysql_test', {
result: result
});
});
module.exports = router;

什么给了?这几乎看起来像是一个缓存问题。我真的希望我的创建/更新操作能够在我的应用程序中立即可见。

源代码:https://github.com/harwoodjp/learning-express

最佳答案

您的问题可能是您没有调用 connection.end() per the docs .

关于mysql - Node/Express + MySQL : Inserts not displaying instantly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40160234/

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