【问题标题】:Updating a mysql data table from query parameters using nodejs and express使用 nodejs 和 express 从查询参数更新 mysql 数据表
【发布时间】:2020-08-31 21:56:08
【问题描述】:

好吧,我想我越来越接近解决这个问题了。现在我正在端口 3000 上运行如下脚本。

curl localhost:3000/register?name=bob\&width=13.970000\&time=0
curl localhost:3000/wheels?left=0.000000\&right=0.000000\&time=0 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=10 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=20 --cookie "USER=bob"
curl localhost:3000/other?ir=0\&time=30 --cookie "USER=bob"
curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=100 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=110 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=120 --cookie "USER=bob"
curl localhost:3000/other?ir=0\&time=130 --cookie "USER=bob"
curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=200 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=210 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=220 --cookie "USER=bob"

然后我使用 nodejs 和 express 将这些查询参数添加到数据库中。一切都在使用'/register',因为我可以在我的数据库中存储'user'和'width'的数据,但是当我尝试从'/wheels 更新相同的表'left1'和'right1'属性时' 那么我能做的最多就是将数据插入到一个全新的表中,其中名称和宽度都恢复为默认值。

最后,我要做的是让应用程序在汽车数据库中创建一个新的数据库条目,填写名称、宽度、left1、right1、时间、dist、l1、l2、l3、ir 数据行,然后在所有这些都填写完毕后在数据库中创建另一个条目......但我现在所能做的就是让它输入大量不同的数据集,这些数据集只有名称(bob)和宽度(14)属性填写..这是我的代码:

const express = require('express');
const session = require('express-session');
const mysql = require('mysql');

let app = express();


var pool = mysql.createPool( {
        host: 'localhost',
        user: 'root',
        password: 'secret',
        database: 'cars',
        connectionLimit: 10,
        multipleStatements : true
});


app.get('/register', function(req, res) {
    pool.query("INSERT INTO cars (`name`, `width`, `time`) VALUES (?,?,?)", [req.query.name, req.query.width, req.query.time]);
});

app.get('/wheels', function (req, res) {
        pool.query("UPDATE `cars` SET `left1`=?, `right1`=?",
        [req.query.left, req.query.right]) });


app.listen(3000, function(req, res) {
        console.log('Express JS ready for port 3000');
});

这是数据库的图片:phpMyAdmin database pic

知道我哪里可能出错了吗?有人告诉我也许可以尝试使用会话和 cookie,但我一直在网上查找诸如“cookie-parser”和“cookie-sessions”之类的东西,但我一直没有使用它们。我不知道如何使用它们对 mysql 数据库做任何事情,而且我在网上找不到任何与我试图用它做的事情相关的东西......太困惑了。任何帮助都会很棒!谢谢

【问题讨论】:

    标签: mysql node.js express


    【解决方案1】:

    根据docs,表/列名周围不应有引号。尝试使用:

    app.get('/wheels', function (req, res) {
            pool.query("UPDATE cars SET left1 = ?, right1 = ?",
            [req.query.left, req.query.right]) 
    });
    

    【讨论】:

    • 谢谢,你说得对,这可能是正确的语法。但它仍然没有用车轮数据更新数据库。对于每个数据库条目,left1 和 right1 都是 0。不过感谢您的帮助!
    • 您可以尝试在您的 /wheels 处理程序中添加console.log(req.query) 吗?
    • 嗯,我应该考虑早点这样做!它们显示为未定义。这是一个问题..当我添加 console.log(req.query.left); console.log(req.query.right);到 /wheels,它们显示为未定义。
    • 我无法使用您的轮子 curl 命令和具有完全相同 /wheels 处理程序的简单快速应用程序(没有会话/cookie 内容)重现此问题。也许您可以禁用会话/cookie 处理只是为了调试?
    • 嗯,我又试了一次,我也无法重新创建它。但是,我认为我取得了一些进展。我添加: res.redirect('/wheels') 作为代码的 app.get('/register', req, res) 部分的最后一个命令,它实际上将 left1 和 right1 数据添加到数据库中,例如我也需要它。这可能是一个丑陋的选择,但也许我可以链接一堆 res.redirect() 命令并让它以这种方式输入?
    猜你喜欢
    • 1970-01-01
    • 2019-04-03
    • 2018-01-02
    • 2017-07-24
    • 2018-10-31
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多