【问题标题】:I am new to node js and I have some problem with post using postman我是节点 js 的新手,我在使用邮递员发帖时遇到了一些问题
【发布时间】:2022-01-24 21:05:48
【问题描述】:

我是 node.js 的新手,并且学习了好几天,现在我被这个 app.post 卡住了,它对我不起作用。如果可能的话,请让我知道如何做 app.update,这样它可能对我的学习过程有很大帮​​助。 敬请期待回复。

const mysql = require('mysql');
const express = require('express');
var app = express();
const bodyparser = require('body-parser');
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.json());
app.listen(8000);
var mysqlconnection = mysql.createConnection(
    {
        host: 'localhost',
        user: 'Naveen',
        password: '',
        database: 'employeedb',
        multipleStatements: true
    }
);

mysqlconnection.connect((err)=>{
    if(!err)
    console.log("DB connection successfull");
    else
    console.log('DB connection failed \n Error : '+ JSON.stringify(err, undefined, 2) );
});
app.post('/employee' ,function (req, res, next) {
    Name = req.query.Name, 
    Empcode = req.query.Empcode,
    Salary = req.query.Salary 
    let sql = "INSERT INTO employee (Name, Empcode, Salary) VALUES (? , ?, ?)";  
    mysqlconnection.query('sql, (Name, Empcode, Salary) ', (err, rows, fields) => {
        if (!err)
            res.send("Insert succeed");
        else
            console.log(err);
        });
    });    
    ```
and then I get these error messages:

**PS C:\xampp\htdocs\first app> node index.js DB connection successfull Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sql, (Name, Empcode, Salary)' at line 1
   (C:\xampp\htdocs\first app\node_modules\express\lib\router\index.js:275:10) {   code: 'ER_PARSE_ERROR',   errno: 1064,   sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sql, (Name, Empcode, Salary)' at line 1",   sqlState: '42000',   index: 0,   sql: 'sql, (Name, Empcode, Salary) ' }**

【问题讨论】:

  • 这是一个 MySQL 查询,因为您在 MySQL 查询中遇到错误。

标签: javascript mysql node.js json post


【解决方案1】:

您的查询中有错字。

// WRONG
mysqlconnection.query('sql, (Name, Empcode, Salary) ', (err, rows, fields)...

它不应该在引号内,变量应该是一个数组。

// CORRECT
mysqlconnection.query(sql, [Name, Empcode, Salary], (err, rows, fields)...

【讨论】:

    【解决方案2】:

    错误很明显。它说“您的 SQL 语法有错误”。你应该在“'sql, (Name, Empcode, Salary)''附近检查你的语法。 这一行在这里

    mysqlconnection.query('sql, (Name, Empcode, Salary) ',
    

    您正在传递字符串文字“sql, (Name, Empcode, Salary)”,而您的意思是传递您在上面一行中创建的变量 sql

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多