如何在Node.js

时间:2017-12-19 11:23:30

标签: javascript mysql node.js

在我注册用户时构建的node.js应用程序中,我会将它们重定向到可以添加汽车的汽车页面。 cars表具有引用用户id列的user_id列。

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

router.post('/register', function(req, res, next) {
    var name = req.body.firstname;
    var email = req.body.email;
    var password = req.body.password;

    const newUser = { name, email, password };

    res.locals.connection.query('INSERT INTO users SET ?', newUser, function(err, row) {
        if (err) {
            console.log(err);
        } else {
            res.redirect('/cars');
        }
    });
});


module.exports = router;

我想知道是否有办法在重定向中传递新插入的ID。这可能吗?这是处理这种问题的最好方法吗?

1 个答案:

答案 0 :(得分:1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="test1">This is a paragraph.</p>
<button id="btn1">Set Text</button>
<p id="test2">This is another paragraph.</p>

<button id="btn2">Set HTML</button>
<p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p>


<button id="btn3">Set Value</button>

在路由器中使用

进行访问
router.post('/register', function(req, res, next) {
    var name = req.body.firstname;
    var email = req.body.email;
    var password = req.body.password;

    const newUser = { name, email, password };

    res.locals.connection.query('INSERT INTO users SET ?', newUser, function(err, row) {
        if (err) {
            console.log(err);
        } else {

            res.redirect('/cars/?id=' + res.insertedId);
        }
    });
});