插入nodejs后更新mysql [英] MySql update after inserting nodejs

查看:73
本文介绍了插入nodejs后更新mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nodejs中用express框架和ejs模板制作了一个简单的应用程序,可以在其中插入mysql数据库并查看结果,但是由于某些原因,当我插入和刷新页面时,即使显示新日期也没有显示将其放入数据库中.我需要关闭服务器以查看新数据.

I made a simple app in nodejs with express framework and ejs template where I can insert into mysql database and see the results, but for some reasons when I insert and refresh the page, the new date is not showing, even if it puts it in the database.I need to close the server to see the new data.

我的index.js路由文件如下所示:

My index.js route file looks like this:

var express = require('express');
var mysql = require('mysql');
var router = express.Router();

var users = [];
var age = [];

var connection = mysql.createConnection( {
    host: 'localhost',
    user: 'admin',
    password: 'pass',
    database: 'users'
});

console.log('\nconnecting to database');
connection.connect();

var query = connection.query('select * from users', function(err, result) {
    if(err) {
        console.error(err);
        return;
    } else {
        for(var i = 0; i<result.length; i++)
        {
            users.push(result[i]['user']);
            age.push(result[i]['age']);
        }
    }
});



/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { 
    title: 'Index page',
    users: users,
    age: age
  });
});


/* GET insert page. */
router.post('/insert', function(req, res, next) {
    var user = req.body.user;
    var age = req.body.age;

    var user_p = {
        user: user,
        age: age
    };

    console.log("post received: %s %s", user, age);

    var query = connection.query('insert into users set ?', user_p, function(err, result) {
        if(err) {
            console.error(err);
            return;
        } else {
            console.log(result);
        }
    });

    res.render('insert', { 
        title: 'Insert page',
        user: user_p
    });
});
// I've removed this connection.end() after I saw the error. But still not getting the new data out of the database when I go back to the index file
connection.end();

module.exports = router;

index.ejs:

The index.ejs:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head><% include partials/template/head.ejs %></head>
    <body>
        <h1><%= title %></h1>
        <p>Welcome to <%= title %></p>

        <ul>
            <% for(var i=0; i<users.length; i++) {%>
                <li>User: <%= users[i] %>, Age: <%= age[i] %></li>
            <% } %>
        </ul>


        <form action="/insert" method="post">
            <input type="text" name="user" placeholder="User"/>
            <input type="text" name="age" placeholder="Age"/>
            <input type="submit" value="send"/>
        </form>


    <% include partials/template/footer.ejs %>
    <% console.log('included footer'); %>
    </body>
</html>

和insert.ejs:

and insert.ejs:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head><% include partials/template/head.ejs %></head>
    <body>
        <h1><%= title %></h1>
        <p>Welcome to <%= title %></p>

    <p>User: <%= user.user %></p>
    <p>Age: <%= user.age %></p>

    <% include partials/template/footer.ejs %>
    <% console.log('included footer'); %>
    </body>
</html>

插入和刷新后如何查看数据库中的更改?

How can I see "live" the changes in my database after I insert and refresh?

P.S.我刚刚看到控制台中出现以下错误:

P.S. I just saw that I have the following error in my console:

{ [Error: Cannot enqueue Query after invoking quit.] code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }

P.P.S.看到错误后,我已经删除了这个connection.end().但是当我回到索引文件时,仍然不能从数据库中获取新数据

P.P.S. I've removed this connection.end() after I saw the error. But still not getting the new data out of the database when I go back to the index file

推荐答案

您似乎从未更新过渲染'/'时使用的数组

you never seem to update the array that are used when rendering '/'

var users = [];
var age = [];

使用从用户中选择*"查询填充一次,但它们永远不会更新.

are filled once using the 'select * from users' query but then they are never updated.

您可以(为了了解正在发生的事情)添加

you could (in order to understand what is going on) add

console.log(result);
users.push(user_p.user);

在插入用户集?"的ok分支中的

.要求.这将显示新用户.

in the ok branch of the 'insert into users set ?' request. This will show the new user.

关于'年龄',您不能简单地做同样的事情,因为您有一个'var age'局部变量遮盖了'var age'模块范围变量.

Regarding the 'age', you cannot simply do the same thing because you have a 'var age' local variable shadowing your 'var age' module scope variable.

这篇关于插入nodejs后更新mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆