node.js - 我正在尝试通过 Node.js 在 MySQL 中发布数据,但无法这样做,因为我找不到错误
问题描述
我已经完成了获取日期和删除数据的工作,但是在发布数据时,我遇到了这个错误。我已经尝试了很多时间,但找不到它。我正在使用 Postman 发布数据。我正面临这个问题,:
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 '=
`name` = 'african', `tagline` = 'black', `description` = 'asthetic', `image...'
at line 1",
sqlState: '42000',
index: 0,"
这是我的代码
const express= require('express')
const bodyParser=require('body-parser')
const mysql=require('mysql')
const app= express()
const port=process.env.PORT || 5000
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
//mysql
const pool=mysql.createPool({
connectionLimit:10,
host :'localhost',
user :'root',
password :'',
database :'nodejs_beers'
})
//Get All beers
app.get('',(req,res)=>{
pool.getConnection((err,connection)=>{
if(err) throw err
console.log(`connected as id ${connection.threadID}`)
connection.query('SELECT * from beers',(err,rows)=>{
connection.release()
if(!err){
res.send(rows)
}
else{
console.log(err)
}
})
})
})
//Get All beers
app.get('/:id',(req,res)=>{
pool.getConnection((err,connection)=>{
if(err) throw err
console.log(`connected as id ${connection.threadID}`)
connection.query('SELECT * from beers WHERE id=?',[req.params.id],(err,rows)=>{
connection.release()
if(!err){
res.send(rows)
}
else{
console.log(err)
}
})
})
})
//delete A beer by id
app.delete('/:id',(req,res)=>{
pool.getConnection((err,connection)=>{
if(err) throw err
console.log(`connected as id ${connection.threadID}`)
connection.query('DELETE from beers WHERE id = ?',[req.params.id],(err,rows)=>{
connection.release()
if(!err){
res.send(`Beer with the id ${[req.params.id]} has been deleted`)
}
else{
console.log(err)
}
})
})
})
//Inser A beer
app.post('',(req,res)=>{
pool.getConnection((err,connection)=>{
if(err) throw err
console.log(`connected as id ${connection.threadID}`)
const params=req.body
connection.query('INSERT INTO beers SET = ?',params,(err,rows)=>{
connection.release()
if(!err){
res.send(`Beer with the id: ${params.id} has been added`)
}
else{
console.log(err)
}
})
console.log(req.body)
})
})
//listen on environment port
app.listen(port,()=>console.log('listen on port $(port)'))
解决方案
你的INSERT INTO... SET...
语法有点不对劲。您已经包含了=
不应该出现的位置(除非您可以链接到另有说明的权威来源)。
相反,您的INSERT
查询应该看起来更像:
connection.query('INSERT INTO beers SET ?',params,(err,rows)=>{ // ...
推荐阅读
- ios - 将背景颜色和占位符图像合并到一张图像中
- wordpress - 在wordpress中的联系表格7中以短URL传递参数
- storage - 如何设置本地存储的配额覆盖openstack中的所有nova计算
- css - Adding shadow to angular material tooltip arrow
- javascript - 从js文件中获取迭代变量
- c# - 将程序集引用添加到项目后,Window.xaml.cs 上的所有代码都出现错误
- javascript - 如何使用 D3.js 在节点之间绘制链接?
- java - Java fx 用户输入如何插入数据库
- docker - 网关 ThingsBoard 配置到 HiveMQ MQTT 代理
- symfony - Symfony 5.3.1 模型字段对当前用户的默认值