javascript - mysql2插入值nodejs

标签 javascript node.js mysql2 node-mysql2

我有这个帖子请求

app.post("/msg", (req, res) => {
  console.log(req.body)
  connection.query('INSERT INTO plans (topic, notes, resources) VALUES 
  (?)', [req.body.topic, req.body.note, req.body.resource],(error, 
  results) => {
     if (error) return res.json({ error: error });

     });
 });

我从中得到了这个错误

"error": {
    "code": "ER_WRONG_VALUE_COUNT_ON_ROW",
    "errno": 1136,
    "sqlState": "21S01",
    "sqlMessage": "Column count doesn't match value count at row 1"
}

这是表格

CREATE TABLE plans(
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  topic VARCHAR(64) NOT NULL,
  notes VARCHAR(200) NOT NULL,
  resources VARCHAR(200) NOT NULL
 );

请问请求有什么问题?

最佳答案

您必须根据您提供的列值的数量提供问号。

app.post("/msg", (req, res) => {
  console.log(req.body)
  connection.query('INSERT INTO plans (topic, notes, resources) VALUES 
  (?,?,?)', [req.body.topic, req.body.note, req.body.resource],(error, 
  results) => {
     if (error) return res.json({ error: error });

     });
 });

这应该可以工作

关于javascript - mysql2插入值nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49487104/

相关文章:

javascript - 如何获取文本区域的旧值

javascript - 插件编程nodeJs

mysql - 为 Ruby 安装 MySQL2 适配器

javascript - 在 PostgreSQL 中存储 Javascript 数字的最佳方式是什么

ruby-on-rails - Ruby On Rails Mysql 访问被拒绝

mysql - 使用ruby远程连接到mysql2

javascript - 互联网浏览器 : HTML entities in URL

javascript - 在 Windows 8 应用程序中使用 Youtube Player API

javascript - 使用 Xml.parse() 删除标签内容与值数组匹配的 html 标签和内容

javascript - Handlebars Helper 返回数组但呈现逗号分隔的字符串