【问题标题】:How to fetch and insert image to Mysql using react,fetchApi and nodejs如何使用 react、fetch Api 和 node js 获取和插入图像到 Mysql
【发布时间】:2019-11-30 07:21:32
【问题描述】:

我想将图像插入到从 接收的 mysql 数据库中,并在获取后希望使用节点将其显示在 上并做出反应

下面是我的nodejs代码。

    const {ISBN,publisher,category,edition,image,title,price,quantity,author,}=req.query
console.log(req.query)
    const insertIntoBooks=`INSERT into BOOKS(ISBN,publisher,category,edition,image,title,price,quantity,author) VALUES('${ISBN}','${publisher}','${category}','${edition}','${image}','${title}',${price},${quantity},'${author}')`
connection.execute(insertIntoBooks).then(resp=>res.json({data:'success'})).catch(err=>res.json({data:'error'}))
})

上面你可以看到我收到了一张图片,这张图片就是我想保存在 Mysql 中的。 下面是我的反应代码



props.startInsertingBook(`http://localhost:3300/books/add?title=${title}&price=${price}&quantity=${quantity}&author=${authorName}&ISBN=${ISBN}&image=${image}&publisher=${publisher}&edition=${edition}&category=${category}`)

下面是我的 redux Action 代码。

return(dispatch)=>{
    return fetch(`${url}`).then(resp=>resp.json()).then(({data})=>{
        console.log(data);
        if(data==='success'){
          return  toastr.success('Success','Book added Successfuly')
        }else{
return toastr.error('Error','Error in submitting')
        }
    }).catch(err=>toastr.error('oops','error occur due to lost in coonectivity'))

}
}```

【问题讨论】:

  • 我相信您共享的fetch 代码将适用于GET 方法,仅适用于POST 方法fetch 采用另一个参数options。您可以查看获取文档here

标签: mysql node.js reactjs redux fetch


【解决方案1】:

由于图片在binary 中,所以不能直接通过get 方法发送。 您需要尝试fetch post 方法来实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-30
    • 2019-01-29
    • 2020-09-07
    • 2019-01-05
    • 2021-08-04
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多