javascript - 我无法将数据插入我的数据库(mysql)
问题描述
我现在正在学习 MySQL,但我遇到了一些问题。我试图向我的数据库发出发布请求::
我来自客户端的代码:
async function sendValues() {
const settings = {
method: 'post',
headers: { "Content-Type": "application/json" },
body: JSON.stringify(product)
}
try {
const fetchResponse = await fetch('http://localhost:9001/products', settings);
const data = await fetchResponse.json();
console.log(data) // what the user send
} catch (err) {
return err
}
}
我的服务器端代码:
router.post('/', (req, res) => {
const { c_id, p_name, p_price } = req.body
const q =
`
INSERT INTO products (c_id,p_name,p_price)
VALUES("${c_id}", "${p_name}" , "${p_price}")
`;
con.query(q, (err, result, fields) => {
if (err) throw err;
res.json(result)
});
});
解决方案
有两个可能的错误:
1.检查客户端和服务器之间的连接。正如刚才提到的。
2.检查表名,属性的数据类型,您提供的属性名称。
推荐阅读
- excel - Excel VBA 'On Error Resume Next' 导致重命名表标题出现问题
- node.js - 使用nodejs来回发送文件
- asp.net-core - 如何使用自定义异常中间件捕获 ASP.NET Core (3.1) URL 解析错误?
- bitbucket - Git - 向旧提交添加更多更改
- java - Spring webflux 何时在平面地图上使用地图
- python - 如何检查在 uWSGI 服务器下工作的两个进程的状态?
- php - PHP将现有的ZIP文件导入数据库
- java - 需要一个 Java 方法的解决方案,它返回一个字符串值,只在 JVM 中执行 n 个线程
- java - 如何在 RecyclerView 中添加搜索过滤器来过滤解析的 JSON 数据?已编辑
- c++ - std::array begin() 和 data() 之间的区别