我正在尝试 MySQL 到 Node.js 中的 post 数据,但无法做到这一点,因为我找不到错误
I am trying to post data in MySQL through Node.js, but unable to do that because I can't find the error
我已经完成了获取日期和删除数据的工作,但是在发布数据时,我遇到了这个错误。我已经尝试了很多时间,但找不到。我正在使用 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...
语法有点不对劲。您在不应该出现的地方包含了 =
(除非您可以 link 到另有说明的权威来源)。
相反,您的 INSERT
查询应该看起来更像:
connection.query('INSERT INTO beers SET ?',params,(err,rows)=>{ // ...
我已经完成了获取日期和删除数据的工作,但是在发布数据时,我遇到了这个错误。我已经尝试了很多时间,但找不到。我正在使用 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...
语法有点不对劲。您在不应该出现的地方包含了 =
(除非您可以 link 到另有说明的权威来源)。
相反,您的 INSERT
查询应该看起来更像:
connection.query('INSERT INTO beers SET ?',params,(err,rows)=>{ // ...